VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAll.cpp@ 105844

Last change on this file since 105844 was 104990, checked in by vboxsync, 5 months ago

VMM/IEM: Added IEMTlbInvalidateAllGlobal and made the IEMTlbInvalidateAll function handle non-global flushes. More TLB stats. bugref:10687

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 153.8 KB
Line 
1/* $Id: PGMAll.cpp 104990 2024-06-20 23:13:34Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor - All context code.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_PGM
33#define VBOX_WITHOUT_PAGING_BIT_FIELDS /* 64-bit bitfields are just asking for trouble. See @bugref{9841} and others. */
34#include <VBox/vmm/pgm.h>
35#include <VBox/vmm/cpum.h>
36#include <VBox/vmm/selm.h>
37#include <VBox/vmm/iem.h>
38#include <VBox/vmm/iom.h>
39#include <VBox/sup.h>
40#include <VBox/vmm/mm.h>
41#include <VBox/vmm/stam.h>
42#include <VBox/vmm/trpm.h>
43#include <VBox/vmm/em.h>
44#include <VBox/vmm/hm.h>
45#include <VBox/vmm/hm_vmx.h>
46#include "PGMInternal.h"
47#include <VBox/vmm/vmcc.h>
48#include "PGMInline.h"
49#include <iprt/assert.h>
50#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
51# include <iprt/asm-amd64-x86.h>
52#endif
53#include <iprt/string.h>
54#include <VBox/log.h>
55#include <VBox/param.h>
56#include <VBox/err.h>
57
58
59/*********************************************************************************************************************************
60* Internal Functions *
61*********************************************************************************************************************************/
62DECLINLINE(int) pgmShwGetLongModePDPtr(PVMCPUCC pVCpu, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPT *ppPdpt, PX86PDPAE *ppPD);
63DECLINLINE(int) pgmShwGetPaePoolPagePD(PVMCPUCC pVCpu, RTGCPTR GCPtr, PPGMPOOLPAGE *ppShwPde);
64DECLINLINE(int) pgmGstMapCr3(PVMCPUCC pVCpu, RTGCPHYS GCPhysCr3, PRTHCPTR pHCPtrGuestCr3);
65#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
66static int pgmGstSlatWalk(PVMCPUCC pVCpu, RTGCPHYS GCPhysNested, bool fIsLinearAddrValid, RTGCPTR GCPtrNested, PPGMPTWALK pWalk,
67 PPGMPTWALKGST pGstWalk);
68static int pgmGstSlatTranslateCr3(PVMCPUCC pVCpu, uint64_t uCr3, PRTGCPHYS pGCPhysCr3);
69static int pgmShwGetNestedEPTPDPtr(PVMCPUCC pVCpu, RTGCPTR64 GCPhysNested, PEPTPDPT *ppPdpt, PEPTPD *ppPD,
70 PPGMPTWALKGST pGstWalkAll);
71#endif
72static int pgmShwSyncLongModePDPtr(PVMCPUCC pVCpu, RTGCPTR64 GCPtr, X86PGPAEUINT uGstPml4e, X86PGPAEUINT uGstPdpe, PX86PDPAE *ppPD);
73static int pgmShwGetEPTPDPtr(PVMCPUCC pVCpu, RTGCPTR64 GCPtr, PEPTPDPT *ppPdpt, PEPTPD *ppPD);
74#ifdef PGM_WITH_PAGE_ZEROING_DETECTION
75static bool pgmHandlePageZeroingCode(PVMCPUCC pVCpu, PCPUMCTX pCtx);
76#endif
77
78
79/*
80 * Second level transation - EPT.
81 */
82#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
83# define PGM_SLAT_TYPE PGM_SLAT_TYPE_EPT
84# include "PGMSlatDefs.h"
85# include "PGMAllGstSlatEpt.cpp.h"
86# undef PGM_SLAT_TYPE
87#endif
88
89
90/*
91 * Shadow - 32-bit mode
92 */
93#define PGM_SHW_TYPE PGM_TYPE_32BIT
94#define PGM_SHW_NAME(name) PGM_SHW_NAME_32BIT(name)
95#include "PGMAllShw.h"
96
97/* Guest - real mode */
98#define PGM_GST_TYPE PGM_TYPE_REAL
99#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
100#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_REAL(name)
101#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_32BIT_PT_FOR_PHYS
102#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_32BIT_PD_PHYS
103#include "PGMGstDefs.h"
104#include "PGMAllGst.h"
105#include "PGMAllBth.h"
106#undef BTH_PGMPOOLKIND_PT_FOR_PT
107#undef BTH_PGMPOOLKIND_ROOT
108#undef PGM_BTH_NAME
109#undef PGM_GST_TYPE
110#undef PGM_GST_NAME
111
112/* Guest - protected mode */
113#define PGM_GST_TYPE PGM_TYPE_PROT
114#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
115#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
116#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_32BIT_PT_FOR_PHYS
117#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_32BIT_PD_PHYS
118#include "PGMGstDefs.h"
119#include "PGMAllGst.h"
120#include "PGMAllBth.h"
121#undef BTH_PGMPOOLKIND_PT_FOR_PT
122#undef BTH_PGMPOOLKIND_ROOT
123#undef PGM_BTH_NAME
124#undef PGM_GST_TYPE
125#undef PGM_GST_NAME
126
127/* Guest - 32-bit mode */
128#define PGM_GST_TYPE PGM_TYPE_32BIT
129#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
130#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_32BIT(name)
131#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT
132#define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB
133#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_32BIT_PD
134#include "PGMGstDefs.h"
135#include "PGMAllGst.h"
136#include "PGMAllBth.h"
137#undef BTH_PGMPOOLKIND_PT_FOR_BIG
138#undef BTH_PGMPOOLKIND_PT_FOR_PT
139#undef BTH_PGMPOOLKIND_ROOT
140#undef PGM_BTH_NAME
141#undef PGM_GST_TYPE
142#undef PGM_GST_NAME
143
144#undef PGM_SHW_TYPE
145#undef PGM_SHW_NAME
146
147
148/*
149 * Shadow - PAE mode
150 */
151#define PGM_SHW_TYPE PGM_TYPE_PAE
152#define PGM_SHW_NAME(name) PGM_SHW_NAME_PAE(name)
153#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_REAL(name)
154#include "PGMAllShw.h"
155
156/* Guest - real mode */
157#define PGM_GST_TYPE PGM_TYPE_REAL
158#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
159#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_REAL(name)
160#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PHYS
161#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PDPT_PHYS
162#include "PGMGstDefs.h"
163#include "PGMAllBth.h"
164#undef BTH_PGMPOOLKIND_PT_FOR_PT
165#undef BTH_PGMPOOLKIND_ROOT
166#undef PGM_BTH_NAME
167#undef PGM_GST_TYPE
168#undef PGM_GST_NAME
169
170/* Guest - protected mode */
171#define PGM_GST_TYPE PGM_TYPE_PROT
172#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
173#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
174#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PHYS
175#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PDPT_PHYS
176#include "PGMGstDefs.h"
177#include "PGMAllBth.h"
178#undef BTH_PGMPOOLKIND_PT_FOR_PT
179#undef BTH_PGMPOOLKIND_ROOT
180#undef PGM_BTH_NAME
181#undef PGM_GST_TYPE
182#undef PGM_GST_NAME
183
184/* Guest - 32-bit mode */
185#define PGM_GST_TYPE PGM_TYPE_32BIT
186#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
187#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_32BIT(name)
188#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_32BIT_PT
189#define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB
190#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PDPT_FOR_32BIT
191#include "PGMGstDefs.h"
192#include "PGMAllBth.h"
193#undef BTH_PGMPOOLKIND_PT_FOR_BIG
194#undef BTH_PGMPOOLKIND_PT_FOR_PT
195#undef BTH_PGMPOOLKIND_ROOT
196#undef PGM_BTH_NAME
197#undef PGM_GST_TYPE
198#undef PGM_GST_NAME
199
200
201/* Guest - PAE mode */
202#define PGM_GST_TYPE PGM_TYPE_PAE
203#define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
204#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PAE(name)
205#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PAE_PT
206#define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_PAE_PT_FOR_PAE_2MB
207#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PDPT
208#include "PGMGstDefs.h"
209#include "PGMAllGst.h"
210#include "PGMAllBth.h"
211#undef BTH_PGMPOOLKIND_PT_FOR_BIG
212#undef BTH_PGMPOOLKIND_PT_FOR_PT
213#undef BTH_PGMPOOLKIND_ROOT
214#undef PGM_BTH_NAME
215#undef PGM_GST_TYPE
216#undef PGM_GST_NAME
217
218#undef PGM_SHW_TYPE
219#undef PGM_SHW_NAME
220
221
222/*
223 * Shadow - AMD64 mode
224 */
225#define PGM_SHW_TYPE PGM_TYPE_AMD64
226#define PGM_SHW_NAME(name) PGM_SHW_NAME_AMD64(name)
227#include "PGMAllShw.h"
228
229/* Guest - protected mode (only used for AMD-V nested paging in 64 bits mode) */
230/** @todo retire this hack. */
231#define PGM_GST_TYPE PGM_TYPE_PROT
232#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
233#define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
234#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PHYS
235#define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_PAE_PD_PHYS
236#include "PGMGstDefs.h"
237#include "PGMAllBth.h"
238#undef BTH_PGMPOOLKIND_PT_FOR_PT
239#undef BTH_PGMPOOLKIND_ROOT
240#undef PGM_BTH_NAME
241#undef PGM_GST_TYPE
242#undef PGM_GST_NAME
243
244#ifdef VBOX_WITH_64_BITS_GUESTS
245/* Guest - AMD64 mode */
246# define PGM_GST_TYPE PGM_TYPE_AMD64
247# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
248# define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_AMD64(name)
249# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_PAE_PT_FOR_PAE_PT
250# define BTH_PGMPOOLKIND_PT_FOR_BIG PGMPOOLKIND_PAE_PT_FOR_PAE_2MB
251# define BTH_PGMPOOLKIND_ROOT PGMPOOLKIND_64BIT_PML4
252# include "PGMGstDefs.h"
253# include "PGMAllGst.h"
254# include "PGMAllBth.h"
255# undef BTH_PGMPOOLKIND_PT_FOR_BIG
256# undef BTH_PGMPOOLKIND_PT_FOR_PT
257# undef BTH_PGMPOOLKIND_ROOT
258# undef PGM_BTH_NAME
259# undef PGM_GST_TYPE
260# undef PGM_GST_NAME
261#endif /* VBOX_WITH_64_BITS_GUESTS */
262
263#undef PGM_SHW_TYPE
264#undef PGM_SHW_NAME
265
266
267/*
268 * Shadow - 32-bit nested paging mode.
269 */
270#define PGM_SHW_TYPE PGM_TYPE_NESTED_32BIT
271#define PGM_SHW_NAME(name) PGM_SHW_NAME_NESTED_32BIT(name)
272#include "PGMAllShw.h"
273
274/* Guest - real mode */
275#define PGM_GST_TYPE PGM_TYPE_REAL
276#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
277#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_32BIT_REAL(name)
278#include "PGMGstDefs.h"
279#include "PGMAllBth.h"
280#undef PGM_BTH_NAME
281#undef PGM_GST_TYPE
282#undef PGM_GST_NAME
283
284/* Guest - protected mode */
285#define PGM_GST_TYPE PGM_TYPE_PROT
286#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
287#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_32BIT_PROT(name)
288#include "PGMGstDefs.h"
289#include "PGMAllBth.h"
290#undef PGM_BTH_NAME
291#undef PGM_GST_TYPE
292#undef PGM_GST_NAME
293
294/* Guest - 32-bit mode */
295#define PGM_GST_TYPE PGM_TYPE_32BIT
296#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
297#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_32BIT_32BIT(name)
298#include "PGMGstDefs.h"
299#include "PGMAllBth.h"
300#undef PGM_BTH_NAME
301#undef PGM_GST_TYPE
302#undef PGM_GST_NAME
303
304/* Guest - PAE mode */
305#define PGM_GST_TYPE PGM_TYPE_PAE
306#define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
307#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_32BIT_PAE(name)
308#include "PGMGstDefs.h"
309#include "PGMAllBth.h"
310#undef PGM_BTH_NAME
311#undef PGM_GST_TYPE
312#undef PGM_GST_NAME
313
314#ifdef VBOX_WITH_64_BITS_GUESTS
315/* Guest - AMD64 mode */
316# define PGM_GST_TYPE PGM_TYPE_AMD64
317# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
318# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_32BIT_AMD64(name)
319# include "PGMGstDefs.h"
320# include "PGMAllBth.h"
321# undef PGM_BTH_NAME
322# undef PGM_GST_TYPE
323# undef PGM_GST_NAME
324#endif /* VBOX_WITH_64_BITS_GUESTS */
325
326#undef PGM_SHW_TYPE
327#undef PGM_SHW_NAME
328
329
330/*
331 * Shadow - PAE nested paging mode.
332 */
333#define PGM_SHW_TYPE PGM_TYPE_NESTED_PAE
334#define PGM_SHW_NAME(name) PGM_SHW_NAME_NESTED_PAE(name)
335#include "PGMAllShw.h"
336
337/* Guest - real mode */
338#define PGM_GST_TYPE PGM_TYPE_REAL
339#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
340#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_PAE_REAL(name)
341#include "PGMGstDefs.h"
342#include "PGMAllBth.h"
343#undef PGM_BTH_NAME
344#undef PGM_GST_TYPE
345#undef PGM_GST_NAME
346
347/* Guest - protected mode */
348#define PGM_GST_TYPE PGM_TYPE_PROT
349#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
350#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_PAE_PROT(name)
351#include "PGMGstDefs.h"
352#include "PGMAllBth.h"
353#undef PGM_BTH_NAME
354#undef PGM_GST_TYPE
355#undef PGM_GST_NAME
356
357/* Guest - 32-bit mode */
358#define PGM_GST_TYPE PGM_TYPE_32BIT
359#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
360#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_PAE_32BIT(name)
361#include "PGMGstDefs.h"
362#include "PGMAllBth.h"
363#undef PGM_BTH_NAME
364#undef PGM_GST_TYPE
365#undef PGM_GST_NAME
366
367/* Guest - PAE mode */
368#define PGM_GST_TYPE PGM_TYPE_PAE
369#define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
370#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_PAE_PAE(name)
371#include "PGMGstDefs.h"
372#include "PGMAllBth.h"
373#undef PGM_BTH_NAME
374#undef PGM_GST_TYPE
375#undef PGM_GST_NAME
376
377#ifdef VBOX_WITH_64_BITS_GUESTS
378/* Guest - AMD64 mode */
379# define PGM_GST_TYPE PGM_TYPE_AMD64
380# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
381# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_PAE_AMD64(name)
382# include "PGMGstDefs.h"
383# include "PGMAllBth.h"
384# undef PGM_BTH_NAME
385# undef PGM_GST_TYPE
386# undef PGM_GST_NAME
387#endif /* VBOX_WITH_64_BITS_GUESTS */
388
389#undef PGM_SHW_TYPE
390#undef PGM_SHW_NAME
391
392
393/*
394 * Shadow - AMD64 nested paging mode.
395 */
396#define PGM_SHW_TYPE PGM_TYPE_NESTED_AMD64
397#define PGM_SHW_NAME(name) PGM_SHW_NAME_NESTED_AMD64(name)
398#include "PGMAllShw.h"
399
400/* Guest - real mode */
401#define PGM_GST_TYPE PGM_TYPE_REAL
402#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
403#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_AMD64_REAL(name)
404#include "PGMGstDefs.h"
405#include "PGMAllBth.h"
406#undef PGM_BTH_NAME
407#undef PGM_GST_TYPE
408#undef PGM_GST_NAME
409
410/* Guest - protected mode */
411#define PGM_GST_TYPE PGM_TYPE_PROT
412#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
413#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_AMD64_PROT(name)
414#include "PGMGstDefs.h"
415#include "PGMAllBth.h"
416#undef PGM_BTH_NAME
417#undef PGM_GST_TYPE
418#undef PGM_GST_NAME
419
420/* Guest - 32-bit mode */
421#define PGM_GST_TYPE PGM_TYPE_32BIT
422#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
423#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_AMD64_32BIT(name)
424#include "PGMGstDefs.h"
425#include "PGMAllBth.h"
426#undef PGM_BTH_NAME
427#undef PGM_GST_TYPE
428#undef PGM_GST_NAME
429
430/* Guest - PAE mode */
431#define PGM_GST_TYPE PGM_TYPE_PAE
432#define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
433#define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_AMD64_PAE(name)
434#include "PGMGstDefs.h"
435#include "PGMAllBth.h"
436#undef PGM_BTH_NAME
437#undef PGM_GST_TYPE
438#undef PGM_GST_NAME
439
440#ifdef VBOX_WITH_64_BITS_GUESTS
441/* Guest - AMD64 mode */
442# define PGM_GST_TYPE PGM_TYPE_AMD64
443# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
444# define PGM_BTH_NAME(name) PGM_BTH_NAME_NESTED_AMD64_AMD64(name)
445# include "PGMGstDefs.h"
446# include "PGMAllBth.h"
447# undef PGM_BTH_NAME
448# undef PGM_GST_TYPE
449# undef PGM_GST_NAME
450#endif /* VBOX_WITH_64_BITS_GUESTS */
451
452#undef PGM_SHW_TYPE
453#undef PGM_SHW_NAME
454
455
456/*
457 * Shadow - EPT.
458 */
459#define PGM_SHW_TYPE PGM_TYPE_EPT
460#define PGM_SHW_NAME(name) PGM_SHW_NAME_EPT(name)
461#include "PGMAllShw.h"
462
463/* Guest - real mode */
464#define PGM_GST_TYPE PGM_TYPE_REAL
465#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
466#define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_REAL(name)
467#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
468#include "PGMGstDefs.h"
469#include "PGMAllBth.h"
470#undef BTH_PGMPOOLKIND_PT_FOR_PT
471#undef PGM_BTH_NAME
472#undef PGM_GST_TYPE
473#undef PGM_GST_NAME
474
475/* Guest - protected mode */
476#define PGM_GST_TYPE PGM_TYPE_PROT
477#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
478#define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
479#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
480#include "PGMGstDefs.h"
481#include "PGMAllBth.h"
482#undef BTH_PGMPOOLKIND_PT_FOR_PT
483#undef PGM_BTH_NAME
484#undef PGM_GST_TYPE
485#undef PGM_GST_NAME
486
487/* Guest - 32-bit mode */
488#define PGM_GST_TYPE PGM_TYPE_32BIT
489#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
490#define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_32BIT(name)
491#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
492#include "PGMGstDefs.h"
493#include "PGMAllBth.h"
494#undef BTH_PGMPOOLKIND_PT_FOR_PT
495#undef PGM_BTH_NAME
496#undef PGM_GST_TYPE
497#undef PGM_GST_NAME
498
499/* Guest - PAE mode */
500#define PGM_GST_TYPE PGM_TYPE_PAE
501#define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
502#define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PAE(name)
503#define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
504#include "PGMGstDefs.h"
505#include "PGMAllBth.h"
506#undef BTH_PGMPOOLKIND_PT_FOR_PT
507#undef PGM_BTH_NAME
508#undef PGM_GST_TYPE
509#undef PGM_GST_NAME
510
511#ifdef VBOX_WITH_64_BITS_GUESTS
512/* Guest - AMD64 mode */
513# define PGM_GST_TYPE PGM_TYPE_AMD64
514# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
515# define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_AMD64(name)
516# define BTH_PGMPOOLKIND_PT_FOR_PT PGMPOOLKIND_EPT_PT_FOR_PHYS
517# include "PGMGstDefs.h"
518# include "PGMAllBth.h"
519# undef BTH_PGMPOOLKIND_PT_FOR_PT
520# undef PGM_BTH_NAME
521# undef PGM_GST_TYPE
522# undef PGM_GST_NAME
523#endif /* VBOX_WITH_64_BITS_GUESTS */
524
525#undef PGM_SHW_TYPE
526#undef PGM_SHW_NAME
527
528
529/*
530 * Shadow - NEM / None.
531 */
532#define PGM_SHW_TYPE PGM_TYPE_NONE
533#define PGM_SHW_NAME(name) PGM_SHW_NAME_NONE(name)
534#include "PGMAllShw.h"
535
536/* Guest - real mode */
537#define PGM_GST_TYPE PGM_TYPE_REAL
538#define PGM_GST_NAME(name) PGM_GST_NAME_REAL(name)
539#define PGM_BTH_NAME(name) PGM_BTH_NAME_NONE_REAL(name)
540#include "PGMGstDefs.h"
541#include "PGMAllBth.h"
542#undef PGM_BTH_NAME
543#undef PGM_GST_TYPE
544#undef PGM_GST_NAME
545
546/* Guest - protected mode */
547#define PGM_GST_TYPE PGM_TYPE_PROT
548#define PGM_GST_NAME(name) PGM_GST_NAME_PROT(name)
549#define PGM_BTH_NAME(name) PGM_BTH_NAME_NONE_PROT(name)
550#include "PGMGstDefs.h"
551#include "PGMAllBth.h"
552#undef PGM_BTH_NAME
553#undef PGM_GST_TYPE
554#undef PGM_GST_NAME
555
556/* Guest - 32-bit mode */
557#define PGM_GST_TYPE PGM_TYPE_32BIT
558#define PGM_GST_NAME(name) PGM_GST_NAME_32BIT(name)
559#define PGM_BTH_NAME(name) PGM_BTH_NAME_NONE_32BIT(name)
560#include "PGMGstDefs.h"
561#include "PGMAllBth.h"
562#undef PGM_BTH_NAME
563#undef PGM_GST_TYPE
564#undef PGM_GST_NAME
565
566/* Guest - PAE mode */
567#define PGM_GST_TYPE PGM_TYPE_PAE
568#define PGM_GST_NAME(name) PGM_GST_NAME_PAE(name)
569#define PGM_BTH_NAME(name) PGM_BTH_NAME_NONE_PAE(name)
570#include "PGMGstDefs.h"
571#include "PGMAllBth.h"
572#undef PGM_BTH_NAME
573#undef PGM_GST_TYPE
574#undef PGM_GST_NAME
575
576#ifdef VBOX_WITH_64_BITS_GUESTS
577/* Guest - AMD64 mode */
578# define PGM_GST_TYPE PGM_TYPE_AMD64
579# define PGM_GST_NAME(name) PGM_GST_NAME_AMD64(name)
580# define PGM_BTH_NAME(name) PGM_BTH_NAME_NONE_AMD64(name)
581# include "PGMGstDefs.h"
582# include "PGMAllBth.h"
583# undef PGM_BTH_NAME
584# undef PGM_GST_TYPE
585# undef PGM_GST_NAME
586#endif /* VBOX_WITH_64_BITS_GUESTS */
587
588#undef PGM_SHW_TYPE
589#undef PGM_SHW_NAME
590
591
592
593/**
594 * Guest mode data array.
595 */
596PGMMODEDATAGST const g_aPgmGuestModeData[PGM_GUEST_MODE_DATA_ARRAY_SIZE] =
597{
598 { UINT32_MAX, NULL, NULL, NULL, NULL }, /* 0 */
599 {
600 PGM_TYPE_REAL,
601 PGM_GST_NAME_REAL(GetPage),
602 PGM_GST_NAME_REAL(QueryPageFast),
603 PGM_GST_NAME_REAL(ModifyPage),
604 PGM_GST_NAME_REAL(Enter),
605 PGM_GST_NAME_REAL(Exit),
606#ifdef IN_RING3
607 PGM_GST_NAME_REAL(Relocate),
608#endif
609 },
610 {
611 PGM_TYPE_PROT,
612 PGM_GST_NAME_PROT(GetPage),
613 PGM_GST_NAME_PROT(QueryPageFast),
614 PGM_GST_NAME_PROT(ModifyPage),
615 PGM_GST_NAME_PROT(Enter),
616 PGM_GST_NAME_PROT(Exit),
617#ifdef IN_RING3
618 PGM_GST_NAME_PROT(Relocate),
619#endif
620 },
621 {
622 PGM_TYPE_32BIT,
623 PGM_GST_NAME_32BIT(GetPage),
624 PGM_GST_NAME_32BIT(QueryPageFast),
625 PGM_GST_NAME_32BIT(ModifyPage),
626 PGM_GST_NAME_32BIT(Enter),
627 PGM_GST_NAME_32BIT(Exit),
628#ifdef IN_RING3
629 PGM_GST_NAME_32BIT(Relocate),
630#endif
631 },
632 {
633 PGM_TYPE_PAE,
634 PGM_GST_NAME_PAE(GetPage),
635 PGM_GST_NAME_PAE(QueryPageFast),
636 PGM_GST_NAME_PAE(ModifyPage),
637 PGM_GST_NAME_PAE(Enter),
638 PGM_GST_NAME_PAE(Exit),
639#ifdef IN_RING3
640 PGM_GST_NAME_PAE(Relocate),
641#endif
642 },
643#ifdef VBOX_WITH_64_BITS_GUESTS
644 {
645 PGM_TYPE_AMD64,
646 PGM_GST_NAME_AMD64(GetPage),
647 PGM_GST_NAME_AMD64(QueryPageFast),
648 PGM_GST_NAME_AMD64(ModifyPage),
649 PGM_GST_NAME_AMD64(Enter),
650 PGM_GST_NAME_AMD64(Exit),
651# ifdef IN_RING3
652 PGM_GST_NAME_AMD64(Relocate),
653# endif
654 },
655#endif
656};
657
658
659/**
660 * The shadow mode data array.
661 */
662PGMMODEDATASHW const g_aPgmShadowModeData[PGM_SHADOW_MODE_DATA_ARRAY_SIZE] =
663{
664 { UINT8_MAX, NULL, NULL, NULL, NULL }, /* 0 */
665 { UINT8_MAX, NULL, NULL, NULL, NULL }, /* PGM_TYPE_REAL */
666 { UINT8_MAX, NULL, NULL, NULL, NULL }, /* PGM_TYPE_PROT */
667 {
668 PGM_TYPE_32BIT,
669 PGM_SHW_NAME_32BIT(GetPage),
670 PGM_SHW_NAME_32BIT(ModifyPage),
671 PGM_SHW_NAME_32BIT(Enter),
672 PGM_SHW_NAME_32BIT(Exit),
673#ifdef IN_RING3
674 PGM_SHW_NAME_32BIT(Relocate),
675#endif
676 },
677 {
678 PGM_TYPE_PAE,
679 PGM_SHW_NAME_PAE(GetPage),
680 PGM_SHW_NAME_PAE(ModifyPage),
681 PGM_SHW_NAME_PAE(Enter),
682 PGM_SHW_NAME_PAE(Exit),
683#ifdef IN_RING3
684 PGM_SHW_NAME_PAE(Relocate),
685#endif
686 },
687 {
688 PGM_TYPE_AMD64,
689 PGM_SHW_NAME_AMD64(GetPage),
690 PGM_SHW_NAME_AMD64(ModifyPage),
691 PGM_SHW_NAME_AMD64(Enter),
692 PGM_SHW_NAME_AMD64(Exit),
693#ifdef IN_RING3
694 PGM_SHW_NAME_AMD64(Relocate),
695#endif
696 },
697 {
698 PGM_TYPE_NESTED_32BIT,
699 PGM_SHW_NAME_NESTED_32BIT(GetPage),
700 PGM_SHW_NAME_NESTED_32BIT(ModifyPage),
701 PGM_SHW_NAME_NESTED_32BIT(Enter),
702 PGM_SHW_NAME_NESTED_32BIT(Exit),
703#ifdef IN_RING3
704 PGM_SHW_NAME_NESTED_32BIT(Relocate),
705#endif
706 },
707 {
708 PGM_TYPE_NESTED_PAE,
709 PGM_SHW_NAME_NESTED_PAE(GetPage),
710 PGM_SHW_NAME_NESTED_PAE(ModifyPage),
711 PGM_SHW_NAME_NESTED_PAE(Enter),
712 PGM_SHW_NAME_NESTED_PAE(Exit),
713#ifdef IN_RING3
714 PGM_SHW_NAME_NESTED_PAE(Relocate),
715#endif
716 },
717 {
718 PGM_TYPE_NESTED_AMD64,
719 PGM_SHW_NAME_NESTED_AMD64(GetPage),
720 PGM_SHW_NAME_NESTED_AMD64(ModifyPage),
721 PGM_SHW_NAME_NESTED_AMD64(Enter),
722 PGM_SHW_NAME_NESTED_AMD64(Exit),
723#ifdef IN_RING3
724 PGM_SHW_NAME_NESTED_AMD64(Relocate),
725#endif
726 },
727 {
728 PGM_TYPE_EPT,
729 PGM_SHW_NAME_EPT(GetPage),
730 PGM_SHW_NAME_EPT(ModifyPage),
731 PGM_SHW_NAME_EPT(Enter),
732 PGM_SHW_NAME_EPT(Exit),
733#ifdef IN_RING3
734 PGM_SHW_NAME_EPT(Relocate),
735#endif
736 },
737 {
738 PGM_TYPE_NONE,
739 PGM_SHW_NAME_NONE(GetPage),
740 PGM_SHW_NAME_NONE(ModifyPage),
741 PGM_SHW_NAME_NONE(Enter),
742 PGM_SHW_NAME_NONE(Exit),
743#ifdef IN_RING3
744 PGM_SHW_NAME_NONE(Relocate),
745#endif
746 },
747};
748
749
750/**
751 * The guest+shadow mode data array.
752 */
753PGMMODEDATABTH const g_aPgmBothModeData[PGM_BOTH_MODE_DATA_ARRAY_SIZE] =
754{
755#if !defined(IN_RING3) && !defined(VBOX_STRICT)
756# define PGMMODEDATABTH_NULL_ENTRY() { UINT32_MAX, UINT32_MAX, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
757# define PGMMODEDATABTH_ENTRY(uShwT, uGstT, Nm) \
758 { uShwT, uGstT, Nm(InvalidatePage), Nm(SyncCR3), Nm(PrefetchPage), Nm(VerifyAccessSyncPage), Nm(MapCR3), Nm(UnmapCR3), Nm(Enter), Nm(Trap0eHandler), Nm(NestedTrap0eHandler) }
759
760#elif !defined(IN_RING3) && defined(VBOX_STRICT)
761# define PGMMODEDATABTH_NULL_ENTRY() { UINT32_MAX, UINT32_MAX, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
762# define PGMMODEDATABTH_ENTRY(uShwT, uGstT, Nm) \
763 { uShwT, uGstT, Nm(InvalidatePage), Nm(SyncCR3), Nm(PrefetchPage), Nm(VerifyAccessSyncPage), Nm(MapCR3), Nm(UnmapCR3), Nm(Enter), Nm(Trap0eHandler), Nm(NestedTrap0eHandler), Nm(AssertCR3) }
764
765#elif defined(IN_RING3) && !defined(VBOX_STRICT)
766# define PGMMODEDATABTH_NULL_ENTRY() { UINT32_MAX, UINT32_MAX, NULL, NULL, NULL, NULL, NULL, NULL }
767# define PGMMODEDATABTH_ENTRY(uShwT, uGstT, Nm) \
768 { uShwT, uGstT, Nm(InvalidatePage), Nm(SyncCR3), Nm(PrefetchPage), Nm(VerifyAccessSyncPage), Nm(MapCR3), Nm(UnmapCR3), Nm(Enter), }
769
770#elif defined(IN_RING3) && defined(VBOX_STRICT)
771# define PGMMODEDATABTH_NULL_ENTRY() { UINT32_MAX, UINT32_MAX, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
772# define PGMMODEDATABTH_ENTRY(uShwT, uGstT, Nm) \
773 { uShwT, uGstT, Nm(InvalidatePage), Nm(SyncCR3), Nm(PrefetchPage), Nm(VerifyAccessSyncPage), Nm(MapCR3), Nm(UnmapCR3), Nm(Enter), Nm(AssertCR3) }
774
775#else
776# error "Misconfig."
777#endif
778
779 /* 32-bit shadow paging mode: */
780 PGMMODEDATABTH_NULL_ENTRY(), /* 0 */
781 PGMMODEDATABTH_ENTRY(PGM_TYPE_32BIT, PGM_TYPE_REAL, PGM_BTH_NAME_32BIT_REAL),
782 PGMMODEDATABTH_ENTRY(PGM_TYPE_32BIT, PGM_TYPE_PROT, PGM_BTH_NAME_32BIT_PROT),
783 PGMMODEDATABTH_ENTRY(PGM_TYPE_32BIT, PGM_TYPE_32BIT, PGM_BTH_NAME_32BIT_32BIT),
784 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_32BIT, PGM_TYPE_PAE - illegal */
785 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_32BIT, PGM_TYPE_AMD64 - illegal */
786 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_32BIT, PGM_TYPE_NESTED_32BIT - illegal */
787 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_32BIT, PGM_TYPE_NESTED_PAE - illegal */
788 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_32BIT, PGM_TYPE_NESTED_AMD64 - illegal */
789 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_32BIT, PGM_TYPE_EPT - illegal */
790 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_32BIT, PGM_TYPE_NONE - illegal */
791
792 /* PAE shadow paging mode: */
793 PGMMODEDATABTH_NULL_ENTRY(), /* 0 */
794 PGMMODEDATABTH_ENTRY(PGM_TYPE_PAE, PGM_TYPE_REAL, PGM_BTH_NAME_PAE_REAL),
795 PGMMODEDATABTH_ENTRY(PGM_TYPE_PAE, PGM_TYPE_PROT, PGM_BTH_NAME_PAE_PROT),
796 PGMMODEDATABTH_ENTRY(PGM_TYPE_PAE, PGM_TYPE_32BIT, PGM_BTH_NAME_PAE_32BIT),
797 PGMMODEDATABTH_ENTRY(PGM_TYPE_PAE, PGM_TYPE_PAE, PGM_BTH_NAME_PAE_PAE),
798 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_PAE, PGM_TYPE_AMD64 - illegal */
799 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_PAE, PGM_TYPE_NESTED_32BIT - illegal */
800 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_PAE, PGM_TYPE_NESTED_PAE - illegal */
801 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_PAE, PGM_TYPE_NESTED_AMD64 - illegal */
802 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_PAE, PGM_TYPE_EPT - illegal */
803 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_PAE, PGM_TYPE_NONE - illegal */
804
805 /* AMD64 shadow paging mode: */
806 PGMMODEDATABTH_NULL_ENTRY(), /* 0 */
807 PGMMODEDATABTH_NULL_ENTRY(), //PGMMODEDATABTH_ENTRY(PGM_TYPE_AMD64, PGM_TYPE_REAL, PGM_BTH_NAME_AMD64_REAL),
808 PGMMODEDATABTH_NULL_ENTRY(), //PGMMODEDATABTH_ENTRY(PGM_TYPE_AMD64, PGM_TYPE_PROT, PGM_BTH_NAME_AMD64_PROT),
809 PGMMODEDATABTH_NULL_ENTRY(), //PGMMODEDATABTH_ENTRY(PGM_TYPE_AMD64, PGM_TYPE_32BIT, PGM_BTH_NAME_AMD64_32BIT),
810 PGMMODEDATABTH_NULL_ENTRY(), //PGMMODEDATABTH_ENTRY(PGM_TYPE_AMD64, PGM_TYPE_PAE, PGM_BTH_NAME_AMD64_PAE),
811#ifdef VBOX_WITH_64_BITS_GUESTS
812 PGMMODEDATABTH_ENTRY(PGM_TYPE_AMD64, PGM_TYPE_AMD64, PGM_BTH_NAME_AMD64_AMD64),
813#else
814 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_AMD64, PGM_TYPE_AMD64 - illegal */
815#endif
816 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_AMD64, PGM_TYPE_NESTED_32BIT - illegal */
817 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_AMD64, PGM_TYPE_NESTED_PAE - illegal */
818 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_AMD64, PGM_TYPE_NESTED_AMD64 - illegal */
819 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_AMD64, PGM_TYPE_EPT - illegal */
820 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_AMD64, PGM_TYPE_NONE - illegal */
821
822 /* 32-bit nested paging mode: */
823 PGMMODEDATABTH_NULL_ENTRY(), /* 0 */
824 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_32BIT, PGM_TYPE_REAL, PGM_BTH_NAME_NESTED_32BIT_REAL),
825 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_32BIT, PGM_TYPE_PROT, PGM_BTH_NAME_NESTED_32BIT_PROT),
826 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_32BIT, PGM_TYPE_32BIT, PGM_BTH_NAME_NESTED_32BIT_32BIT),
827 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_32BIT, PGM_TYPE_PAE, PGM_BTH_NAME_NESTED_32BIT_PAE),
828#ifdef VBOX_WITH_64_BITS_GUESTS
829 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_32BIT, PGM_TYPE_AMD64, PGM_BTH_NAME_NESTED_32BIT_AMD64),
830#else
831 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_32BIT, PGM_TYPE_AMD64 - illegal */
832#endif
833 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_32BIT, PGM_TYPE_NESTED_32BIT - illegal */
834 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_32BIT, PGM_TYPE_NESTED_PAE - illegal */
835 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_32BIT, PGM_TYPE_NESTED_AMD64 - illegal */
836 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_32BIT, PGM_TYPE_EPT - illegal */
837 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_32BIT, PGM_TYPE_NONE - illegal */
838
839 /* PAE nested paging mode: */
840 PGMMODEDATABTH_NULL_ENTRY(), /* 0 */
841 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_PAE, PGM_TYPE_REAL, PGM_BTH_NAME_NESTED_PAE_REAL),
842 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_PAE, PGM_TYPE_PROT, PGM_BTH_NAME_NESTED_PAE_PROT),
843 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_PAE, PGM_TYPE_32BIT, PGM_BTH_NAME_NESTED_PAE_32BIT),
844 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_PAE, PGM_TYPE_PAE, PGM_BTH_NAME_NESTED_PAE_PAE),
845#ifdef VBOX_WITH_64_BITS_GUESTS
846 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_PAE, PGM_TYPE_AMD64, PGM_BTH_NAME_NESTED_PAE_AMD64),
847#else
848 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_PAE, PGM_TYPE_AMD64 - illegal */
849#endif
850 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_PAE, PGM_TYPE_NESTED_32BIT - illegal */
851 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_PAE, PGM_TYPE_NESTED_PAE - illegal */
852 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_PAE, PGM_TYPE_NESTED_AMD64 - illegal */
853 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_PAE, PGM_TYPE_EPT - illegal */
854 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_PAE, PGM_TYPE_NONE - illegal */
855
856 /* AMD64 nested paging mode: */
857 PGMMODEDATABTH_NULL_ENTRY(), /* 0 */
858 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_AMD64, PGM_TYPE_REAL, PGM_BTH_NAME_NESTED_AMD64_REAL),
859 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_AMD64, PGM_TYPE_PROT, PGM_BTH_NAME_NESTED_AMD64_PROT),
860 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_AMD64, PGM_TYPE_32BIT, PGM_BTH_NAME_NESTED_AMD64_32BIT),
861 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_AMD64, PGM_TYPE_PAE, PGM_BTH_NAME_NESTED_AMD64_PAE),
862#ifdef VBOX_WITH_64_BITS_GUESTS
863 PGMMODEDATABTH_ENTRY(PGM_TYPE_NESTED_AMD64, PGM_TYPE_AMD64, PGM_BTH_NAME_NESTED_AMD64_AMD64),
864#else
865 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_AMD64, PGM_TYPE_AMD64 - illegal */
866#endif
867 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_AMD64, PGM_TYPE_NESTED_32BIT - illegal */
868 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_AMD64, PGM_TYPE_NESTED_PAE - illegal */
869 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_AMD64, PGM_TYPE_NESTED_AMD64 - illegal */
870 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_AMD64, PGM_TYPE_EPT - illegal */
871 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NESTED_AMD64, PGM_TYPE_NONE - illegal */
872
873 /* EPT nested paging mode: */
874 PGMMODEDATABTH_NULL_ENTRY(), /* 0 */
875 PGMMODEDATABTH_ENTRY(PGM_TYPE_EPT, PGM_TYPE_REAL, PGM_BTH_NAME_EPT_REAL),
876 PGMMODEDATABTH_ENTRY(PGM_TYPE_EPT, PGM_TYPE_PROT, PGM_BTH_NAME_EPT_PROT),
877 PGMMODEDATABTH_ENTRY(PGM_TYPE_EPT, PGM_TYPE_32BIT, PGM_BTH_NAME_EPT_32BIT),
878 PGMMODEDATABTH_ENTRY(PGM_TYPE_EPT, PGM_TYPE_PAE, PGM_BTH_NAME_EPT_PAE),
879#ifdef VBOX_WITH_64_BITS_GUESTS
880 PGMMODEDATABTH_ENTRY(PGM_TYPE_EPT, PGM_TYPE_AMD64, PGM_BTH_NAME_EPT_AMD64),
881#else
882 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_EPT, PGM_TYPE_AMD64 - illegal */
883#endif
884 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_EPT, PGM_TYPE_NESTED_32BIT - illegal */
885 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_EPT, PGM_TYPE_NESTED_PAE - illegal */
886 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_EPT, PGM_TYPE_NESTED_AMD64 - illegal */
887 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_EPT, PGM_TYPE_EPT - illegal */
888 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_EPT, PGM_TYPE_NONE - illegal */
889
890 /* NONE / NEM: */
891 PGMMODEDATABTH_NULL_ENTRY(), /* 0 */
892 PGMMODEDATABTH_ENTRY(PGM_TYPE_NONE, PGM_TYPE_REAL, PGM_BTH_NAME_EPT_REAL),
893 PGMMODEDATABTH_ENTRY(PGM_TYPE_NONE, PGM_TYPE_PROT, PGM_BTH_NAME_EPT_PROT),
894 PGMMODEDATABTH_ENTRY(PGM_TYPE_NONE, PGM_TYPE_32BIT, PGM_BTH_NAME_EPT_32BIT),
895 PGMMODEDATABTH_ENTRY(PGM_TYPE_NONE, PGM_TYPE_PAE, PGM_BTH_NAME_EPT_PAE),
896#ifdef VBOX_WITH_64_BITS_GUESTS
897 PGMMODEDATABTH_ENTRY(PGM_TYPE_NONE, PGM_TYPE_AMD64, PGM_BTH_NAME_EPT_AMD64),
898#else
899 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NONE, PGM_TYPE_AMD64 - illegal */
900#endif
901 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NONE, PGM_TYPE_NESTED_32BIT - illegal */
902 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NONE, PGM_TYPE_NESTED_PAE - illegal */
903 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NONE, PGM_TYPE_NESTED_AMD64 - illegal */
904 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NONE, PGM_TYPE_EPT - illegal */
905 PGMMODEDATABTH_NULL_ENTRY(), /* PGM_TYPE_NONE, PGM_TYPE_NONE - illegal */
906
907
908#undef PGMMODEDATABTH_ENTRY
909#undef PGMMODEDATABTH_NULL_ENTRY
910};
911
912
913/** Mask array used by pgmGetCr3MaskForMode.
914 * X86_CR3_AMD64_PAGE_MASK is used for modes that doesn't have a CR3 or EPTP. */
915static uint64_t const g_auCr3MaskForMode[PGMMODE_MAX] =
916{
917 /* [PGMMODE_INVALID] = */ X86_CR3_AMD64_PAGE_MASK,
918 /* [PGMMODE_REAL] = */ X86_CR3_AMD64_PAGE_MASK,
919 /* [PGMMODE_PROTECTED] = */ X86_CR3_AMD64_PAGE_MASK,
920 /* [PGMMODE_32_BIT] = */ X86_CR3_PAGE_MASK,
921 /* [PGMMODE_PAE] = */ X86_CR3_PAE_PAGE_MASK,
922 /* [PGMMODE_PAE_NX] = */ X86_CR3_PAE_PAGE_MASK,
923 /* [PGMMODE_AMD64] = */ X86_CR3_AMD64_PAGE_MASK,
924 /* [PGMMODE_AMD64_NX] = */ X86_CR3_AMD64_PAGE_MASK,
925 /* [PGMMODE_NESTED_32BIT = */ X86_CR3_PAGE_MASK,
926 /* [PGMMODE_NESTED_PAE] = */ X86_CR3_PAE_PAGE_MASK,
927 /* [PGMMODE_NESTED_AMD64] = */ X86_CR3_AMD64_PAGE_MASK,
928 /* [PGMMODE_EPT] = */ X86_CR3_EPT_PAGE_MASK,
929 /* [PGMMODE_NONE] = */ X86_CR3_AMD64_PAGE_MASK,
930};
931
932
933/**
934 * Gets the physical address mask for CR3 in the given paging mode.
935 *
936 * The mask is for eliminating flags and other stuff in CR3/EPTP when
937 * extracting the physical address. It is not for validating whether there are
938 * reserved bits set. PGM ASSUMES that whoever loaded the CR3 value and passed
939 * it to PGM checked for reserved bits, including reserved physical address
940 * bits.
941 *
942 * @returns The CR3 mask.
943 * @param enmMode The paging mode.
944 * @param enmSlatMode The second-level address translation mode.
945 */
946DECLINLINE(uint64_t) pgmGetCr3MaskForMode(PGMMODE enmMode, PGMSLAT enmSlatMode)
947{
948 if (enmSlatMode == PGMSLAT_DIRECT)
949 {
950 Assert(enmMode != PGMMODE_EPT);
951 return g_auCr3MaskForMode[(unsigned)enmMode < (unsigned)PGMMODE_MAX ? enmMode : 0];
952 }
953 Assert(enmSlatMode == PGMSLAT_EPT);
954 return X86_CR3_EPT_PAGE_MASK;
955}
956
957
958/**
959 * Gets the masked CR3 value according to the current guest paging mode.
960 *
961 * See disclaimer in pgmGetCr3MaskForMode.
962 *
963 * @returns The masked PGM CR3 value.
964 * @param pVCpu The cross context virtual CPU structure.
965 * @param uCr3 The raw guest CR3 value.
966 */
967DECLINLINE(RTGCPHYS) pgmGetGuestMaskedCr3(PVMCPUCC pVCpu, uint64_t uCr3)
968{
969 uint64_t const fCr3Mask = pgmGetCr3MaskForMode(pVCpu->pgm.s.enmGuestMode, pVCpu->pgm.s.enmGuestSlatMode);
970 RTGCPHYS GCPhysCR3 = (RTGCPHYS)(uCr3 & fCr3Mask);
971 PGM_A20_APPLY_TO_VAR(pVCpu, GCPhysCR3);
972 return GCPhysCR3;
973}
974
975
976#ifdef IN_RING0
977/**
978 * #PF Handler.
979 *
980 * @returns VBox status code (appropriate for trap handling and GC return).
981 * @param pVCpu The cross context virtual CPU structure.
982 * @param uErr The trap error code.
983 * @param pCtx Pointer to the register context for the CPU.
984 * @param pvFault The fault address.
985 */
986VMMDECL(int) PGMTrap0eHandler(PVMCPUCC pVCpu, RTGCUINT uErr, PCPUMCTX pCtx, RTGCPTR pvFault)
987{
988 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
989
990 Log(("PGMTrap0eHandler: uErr=%RGx pvFault=%RGv eip=%04x:%RGv cr3=%RGp\n", uErr, pvFault, pCtx->cs.Sel, (RTGCPTR)pCtx->rip, (RTGCPHYS)CPUMGetGuestCR3(pVCpu)));
991 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.StatRZTrap0e, a);
992 STAM_STATS({ pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = NULL; } );
993
994
995# ifdef VBOX_WITH_STATISTICS
996 /*
997 * Error code stats.
998 */
999 if (uErr & X86_TRAP_PF_US)
1000 {
1001 if (!(uErr & X86_TRAP_PF_P))
1002 {
1003 if (uErr & X86_TRAP_PF_RW)
1004 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eUSNotPresentWrite);
1005 else
1006 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eUSNotPresentRead);
1007 }
1008 else if (uErr & X86_TRAP_PF_RW)
1009 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eUSWrite);
1010 else if (uErr & X86_TRAP_PF_RSVD)
1011 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eUSReserved);
1012 else if (uErr & X86_TRAP_PF_ID)
1013 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eUSNXE);
1014 else
1015 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eUSRead);
1016 }
1017 else
1018 { /* Supervisor */
1019 if (!(uErr & X86_TRAP_PF_P))
1020 {
1021 if (uErr & X86_TRAP_PF_RW)
1022 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eSVNotPresentWrite);
1023 else
1024 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eSVNotPresentRead);
1025 }
1026 else if (uErr & X86_TRAP_PF_RW)
1027 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eSVWrite);
1028 else if (uErr & X86_TRAP_PF_ID)
1029 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eSNXE);
1030 else if (uErr & X86_TRAP_PF_RSVD)
1031 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eSVReserved);
1032 }
1033# endif /* VBOX_WITH_STATISTICS */
1034
1035 /*
1036 * Call the worker.
1037 */
1038 uintptr_t const idxBth = pVCpu->pgm.s.idxBothModeData;
1039 AssertReturn(idxBth < RT_ELEMENTS(g_aPgmBothModeData), VERR_PGM_MODE_IPE);
1040 AssertReturn(g_aPgmBothModeData[idxBth].pfnTrap0eHandler, VERR_PGM_MODE_IPE);
1041 bool fLockTaken = false;
1042 int rc = g_aPgmBothModeData[idxBth].pfnTrap0eHandler(pVCpu, uErr, pCtx, pvFault, &fLockTaken);
1043 if (fLockTaken)
1044 {
1045 PGM_LOCK_ASSERT_OWNER(pVM);
1046 PGM_UNLOCK(pVM);
1047 }
1048 LogFlow(("PGMTrap0eHandler: uErr=%RGx pvFault=%RGv rc=%Rrc\n", uErr, pvFault, rc));
1049
1050 /*
1051 * Return code tweaks.
1052 */
1053 if (rc != VINF_SUCCESS)
1054 {
1055 if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
1056 rc = VINF_SUCCESS;
1057
1058 /* Note: hack alert for difficult to reproduce problem. */
1059 if ( rc == VERR_PAGE_NOT_PRESENT /* SMP only ; disassembly might fail. */
1060 || rc == VERR_PAGE_TABLE_NOT_PRESENT /* seen with UNI & SMP */
1061 || rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT /* seen with SMP */
1062 || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT) /* precaution */
1063 {
1064 Log(("WARNING: Unexpected VERR_PAGE_TABLE_NOT_PRESENT (%d) for page fault at %RGv error code %x (rip=%RGv)\n", rc, pvFault, uErr, pCtx->rip));
1065 /* Some kind of inconsistency in the SMP case; it's safe to just execute the instruction again; not sure about single VCPU VMs though. */
1066 rc = VINF_SUCCESS;
1067 }
1068 }
1069
1070 STAM_STATS({ if (rc == VINF_EM_RAW_GUEST_TRAP) STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.StatRZTrap0eGuestPF); });
1071 STAM_STATS({ if (!pVCpu->pgmr0.s.pStatTrap0eAttributionR0)
1072 pVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pVCpu->pgm.s.Stats.StatRZTrap0eTime2Misc; });
1073 STAM_PROFILE_STOP_EX(&pVCpu->pgm.s.Stats.StatRZTrap0e, pVCpu->pgmr0.s.pStatTrap0eAttributionR0, a);
1074 return rc;
1075}
1076#endif /* IN_RING0 */
1077
1078
1079/**
1080 * Prefetch a page
1081 *
1082 * Typically used to sync commonly used pages before entering raw mode
1083 * after a CR3 reload.
1084 *
1085 * @returns VBox status code suitable for scheduling.
1086 * @retval VINF_SUCCESS on success.
1087 * @retval VINF_PGM_SYNC_CR3 if we're out of shadow pages or something like that.
1088 * @param pVCpu The cross context virtual CPU structure.
1089 * @param GCPtrPage Page to invalidate.
1090 */
1091VMMDECL(int) PGMPrefetchPage(PVMCPUCC pVCpu, RTGCPTR GCPtrPage)
1092{
1093 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,Prefetch), a);
1094
1095 uintptr_t const idxBth = pVCpu->pgm.s.idxBothModeData;
1096 AssertReturn(idxBth < RT_ELEMENTS(g_aPgmBothModeData), VERR_PGM_MODE_IPE);
1097 AssertReturn(g_aPgmBothModeData[idxBth].pfnPrefetchPage, VERR_PGM_MODE_IPE);
1098 int rc = g_aPgmBothModeData[idxBth].pfnPrefetchPage(pVCpu, GCPtrPage);
1099
1100 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,Prefetch), a);
1101 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 || RT_FAILURE(rc), ("rc=%Rrc\n", rc));
1102 return rc;
1103}
1104
1105
1106/**
1107 * Emulation of the invlpg instruction (HC only actually).
1108 *
1109 * @returns Strict VBox status code, special care required.
1110 * @retval VINF_PGM_SYNC_CR3 - handled.
1111 * @retval VINF_EM_RAW_EMULATE_INSTR - not handled (RC only).
1112 *
1113 * @param pVCpu The cross context virtual CPU structure.
1114 * @param GCPtrPage Page to invalidate.
1115 *
1116 * @remark ASSUMES the page table entry or page directory is valid. Fairly
1117 * safe, but there could be edge cases!
1118 *
1119 * @todo Flush page or page directory only if necessary!
1120 * @todo VBOXSTRICTRC
1121 */
1122VMMDECL(int) PGMInvalidatePage(PVMCPUCC pVCpu, RTGCPTR GCPtrPage)
1123{
1124 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1125 int rc;
1126 Log3(("PGMInvalidatePage: GCPtrPage=%RGv\n", GCPtrPage));
1127
1128 IEMTlbInvalidatePage(pVCpu, GCPtrPage);
1129
1130 /*
1131 * Call paging mode specific worker.
1132 */
1133 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePage), a);
1134 PGM_LOCK_VOID(pVM);
1135
1136 uintptr_t const idxBth = pVCpu->pgm.s.idxBothModeData;
1137 AssertReturnStmt(idxBth < RT_ELEMENTS(g_aPgmBothModeData), PGM_UNLOCK(pVM), VERR_PGM_MODE_IPE);
1138 AssertReturnStmt(g_aPgmBothModeData[idxBth].pfnInvalidatePage, PGM_UNLOCK(pVM), VERR_PGM_MODE_IPE);
1139 rc = g_aPgmBothModeData[idxBth].pfnInvalidatePage(pVCpu, GCPtrPage);
1140
1141 PGM_UNLOCK(pVM);
1142 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,InvalidatePage), a);
1143
1144 /* Ignore all irrelevant error codes. */
1145 if ( rc == VERR_PAGE_NOT_PRESENT
1146 || rc == VERR_PAGE_TABLE_NOT_PRESENT
1147 || rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT
1148 || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT)
1149 rc = VINF_SUCCESS;
1150
1151 return rc;
1152}
1153
1154
1155/**
1156 * Executes an instruction using the interpreter.
1157 *
1158 * @returns VBox status code (appropriate for trap handling and GC return).
1159 * @param pVCpu The cross context virtual CPU structure.
1160 * @param pvFault Fault address.
1161 */
1162VMMDECL(VBOXSTRICTRC) PGMInterpretInstruction(PVMCPUCC pVCpu, RTGCPTR pvFault)
1163{
1164 RT_NOREF(pvFault);
1165 VBOXSTRICTRC rc = EMInterpretInstruction(pVCpu);
1166 if (rc == VERR_EM_INTERPRETER)
1167 rc = VINF_EM_RAW_EMULATE_INSTR;
1168 if (rc != VINF_SUCCESS)
1169 Log(("PGMInterpretInstruction: returns %Rrc (pvFault=%RGv)\n", VBOXSTRICTRC_VAL(rc), pvFault));
1170 return rc;
1171}
1172
1173
1174/**
1175 * Gets effective page information (from the VMM page directory).
1176 *
1177 * @returns VBox status code.
1178 * @param pVCpu The cross context virtual CPU structure.
1179 * @param GCPtr Guest Context virtual address of the page.
1180 * @param pfFlags Where to store the flags. These are X86_PTE_*.
1181 * @param pHCPhys Where to store the HC physical address of the page.
1182 * This is page aligned.
1183 * @remark You should use PGMMapGetPage() for pages in a mapping.
1184 */
1185VMMDECL(int) PGMShwGetPage(PVMCPUCC pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys)
1186{
1187 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1188 PGM_LOCK_VOID(pVM);
1189
1190 uintptr_t idxShw = pVCpu->pgm.s.idxShadowModeData;
1191 AssertReturn(idxShw < RT_ELEMENTS(g_aPgmShadowModeData), VERR_PGM_MODE_IPE);
1192 AssertReturn(g_aPgmShadowModeData[idxShw].pfnGetPage, VERR_PGM_MODE_IPE);
1193 int rc = g_aPgmShadowModeData[idxShw].pfnGetPage(pVCpu, GCPtr, pfFlags, pHCPhys);
1194
1195 PGM_UNLOCK(pVM);
1196 return rc;
1197}
1198
1199
1200/**
1201 * Modify page flags for a range of pages in the shadow context.
1202 *
1203 * The existing flags are ANDed with the fMask and ORed with the fFlags.
1204 *
1205 * @returns VBox status code.
1206 * @param pVCpu The cross context virtual CPU structure.
1207 * @param GCPtr Virtual address of the first page in the range.
1208 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
1209 * @param fMask The AND mask - page flags X86_PTE_*.
1210 * Be very CAREFUL when ~'ing constants which could be 32-bit!
1211 * @param fOpFlags A combination of the PGM_MK_PK_XXX flags.
1212 * @remark You must use PGMMapModifyPage() for pages in a mapping.
1213 */
1214DECLINLINE(int) pdmShwModifyPage(PVMCPUCC pVCpu, RTGCPTR GCPtr, uint64_t fFlags, uint64_t fMask, uint32_t fOpFlags)
1215{
1216 AssertMsg(!(fFlags & X86_PTE_PAE_PG_MASK), ("fFlags=%#llx\n", fFlags));
1217 Assert(!(fOpFlags & ~(PGM_MK_PG_IS_MMIO2 | PGM_MK_PG_IS_WRITE_FAULT)));
1218
1219 GCPtr &= ~(RTGCPTR)GUEST_PAGE_OFFSET_MASK; /** @todo this ain't necessary, right... */
1220
1221 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1222 PGM_LOCK_VOID(pVM);
1223
1224 uintptr_t idxShw = pVCpu->pgm.s.idxShadowModeData;
1225 AssertReturn(idxShw < RT_ELEMENTS(g_aPgmShadowModeData), VERR_PGM_MODE_IPE);
1226 AssertReturn(g_aPgmShadowModeData[idxShw].pfnModifyPage, VERR_PGM_MODE_IPE);
1227 int rc = g_aPgmShadowModeData[idxShw].pfnModifyPage(pVCpu, GCPtr, GUEST_PAGE_SIZE, fFlags, fMask, fOpFlags);
1228
1229 PGM_UNLOCK(pVM);
1230 return rc;
1231}
1232
1233
1234/**
1235 * Changing the page flags for a single page in the shadow page tables so as to
1236 * make it read-only.
1237 *
1238 * @returns VBox status code.
1239 * @param pVCpu The cross context virtual CPU structure.
1240 * @param GCPtr Virtual address of the first page in the range.
1241 * @param fOpFlags A combination of the PGM_MK_PK_XXX flags.
1242 */
1243VMMDECL(int) PGMShwMakePageReadonly(PVMCPUCC pVCpu, RTGCPTR GCPtr, uint32_t fOpFlags)
1244{
1245 return pdmShwModifyPage(pVCpu, GCPtr, 0, ~(uint64_t)X86_PTE_RW, fOpFlags);
1246}
1247
1248
1249/**
1250 * Changing the page flags for a single page in the shadow page tables so as to
1251 * make it writable.
1252 *
1253 * The call must know with 101% certainty that the guest page tables maps this
1254 * as writable too. This function will deal shared, zero and write monitored
1255 * pages.
1256 *
1257 * @returns VBox status code.
1258 * @param pVCpu The cross context virtual CPU structure.
1259 * @param GCPtr Virtual address of the first page in the range.
1260 * @param fOpFlags A combination of the PGM_MK_PK_XXX flags.
1261 */
1262VMMDECL(int) PGMShwMakePageWritable(PVMCPUCC pVCpu, RTGCPTR GCPtr, uint32_t fOpFlags)
1263{
1264 if (pVCpu->pgm.s.enmShadowMode != PGMMODE_NONE) /* avoid assertions */
1265 return pdmShwModifyPage(pVCpu, GCPtr, X86_PTE_RW, ~(uint64_t)0, fOpFlags);
1266 return VINF_SUCCESS;
1267}
1268
1269
1270/**
1271 * Changing the page flags for a single page in the shadow page tables so as to
1272 * make it not present.
1273 *
1274 * @returns VBox status code.
1275 * @param pVCpu The cross context virtual CPU structure.
1276 * @param GCPtr Virtual address of the first page in the range.
1277 * @param fOpFlags A combination of the PGM_MK_PG_XXX flags.
1278 */
1279VMMDECL(int) PGMShwMakePageNotPresent(PVMCPUCC pVCpu, RTGCPTR GCPtr, uint32_t fOpFlags)
1280{
1281 return pdmShwModifyPage(pVCpu, GCPtr, 0, 0, fOpFlags);
1282}
1283
1284
1285/**
1286 * Changing the page flags for a single page in the shadow page tables so as to
1287 * make it supervisor and writable.
1288 *
1289 * This if for dealing with CR0.WP=0 and readonly user pages.
1290 *
1291 * @returns VBox status code.
1292 * @param pVCpu The cross context virtual CPU structure.
1293 * @param GCPtr Virtual address of the first page in the range.
1294 * @param fBigPage Whether or not this is a big page. If it is, we have to
1295 * change the shadow PDE as well. If it isn't, the caller
1296 * has checked that the shadow PDE doesn't need changing.
1297 * We ASSUME 4KB pages backing the big page here!
1298 * @param fOpFlags A combination of the PGM_MK_PG_XXX flags.
1299 */
1300int pgmShwMakePageSupervisorAndWritable(PVMCPUCC pVCpu, RTGCPTR GCPtr, bool fBigPage, uint32_t fOpFlags)
1301{
1302 int rc = pdmShwModifyPage(pVCpu, GCPtr, X86_PTE_RW, ~(uint64_t)X86_PTE_US, fOpFlags);
1303 if (rc == VINF_SUCCESS && fBigPage)
1304 {
1305 /* this is a bit ugly... */
1306 switch (pVCpu->pgm.s.enmShadowMode)
1307 {
1308 case PGMMODE_32_BIT:
1309 {
1310 PX86PDE pPde = pgmShwGet32BitPDEPtr(pVCpu, GCPtr);
1311 AssertReturn(pPde, VERR_INTERNAL_ERROR_3);
1312 Log(("pgmShwMakePageSupervisorAndWritable: PDE=%#llx", pPde->u));
1313 pPde->u |= X86_PDE_RW;
1314 Log(("-> PDE=%#llx (32)\n", pPde->u));
1315 break;
1316 }
1317 case PGMMODE_PAE:
1318 case PGMMODE_PAE_NX:
1319 {
1320 PX86PDEPAE pPde = pgmShwGetPaePDEPtr(pVCpu, GCPtr);
1321 AssertReturn(pPde, VERR_INTERNAL_ERROR_3);
1322 Log(("pgmShwMakePageSupervisorAndWritable: PDE=%#llx", pPde->u));
1323 pPde->u |= X86_PDE_RW;
1324 Log(("-> PDE=%#llx (PAE)\n", pPde->u));
1325 break;
1326 }
1327 default:
1328 AssertFailedReturn(VERR_INTERNAL_ERROR_4);
1329 }
1330 }
1331 return rc;
1332}
1333
1334
1335/**
1336 * Gets the shadow page directory for the specified address, PAE.
1337 *
1338 * @returns Pointer to the shadow PD.
1339 * @param pVCpu The cross context virtual CPU structure.
1340 * @param GCPtr The address.
1341 * @param uGstPdpe Guest PDPT entry. Valid.
1342 * @param ppPD Receives address of page directory
1343 */
1344int pgmShwSyncPaePDPtr(PVMCPUCC pVCpu, RTGCPTR GCPtr, X86PGPAEUINT uGstPdpe, PX86PDPAE *ppPD)
1345{
1346 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1347 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1348 PPGMPOOLPAGE pShwPage;
1349 int rc;
1350 PGM_LOCK_ASSERT_OWNER(pVM);
1351
1352
1353 /* Allocate page directory if not present. */
1354 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
1355 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(pVCpu);
1356 PX86PDPE pPdpe = &pPdpt->a[iPdPt];
1357 X86PGPAEUINT const uPdpe = pPdpe->u;
1358 if (uPdpe & (X86_PDPE_P | X86_PDPE_PG_MASK))
1359 {
1360 pShwPage = pgmPoolGetPage(pPool, uPdpe & X86_PDPE_PG_MASK);
1361 AssertReturn(pShwPage, VERR_PGM_POOL_GET_PAGE_FAILED);
1362 Assert((pPdpe->u & X86_PDPE_PG_MASK) == pShwPage->Core.Key);
1363
1364 pgmPoolCacheUsed(pPool, pShwPage);
1365
1366 /* Update the entry if necessary. */
1367 X86PGPAEUINT const uPdpeNew = pShwPage->Core.Key | (uGstPdpe & (X86_PDPE_P | X86_PDPE_A)) | (uPdpe & PGM_PDPT_FLAGS);
1368 if (uPdpeNew == uPdpe)
1369 { /* likely */ }
1370 else
1371 ASMAtomicWriteU64(&pPdpe->u, uPdpeNew);
1372 }
1373 else
1374 {
1375 RTGCPTR64 GCPdPt;
1376 PGMPOOLKIND enmKind;
1377 if (pVM->pgm.s.fNestedPaging || !CPUMIsGuestPagingEnabled(pVCpu))
1378 {
1379 /* AMD-V nested paging or real/protected mode without paging. */
1380 GCPdPt = GCPtr & ~(RT_BIT_64(X86_PDPT_SHIFT) - 1);
1381 enmKind = PGMPOOLKIND_PAE_PD_PHYS;
1382 }
1383 else if (CPUMGetGuestCR4(pVCpu) & X86_CR4_PAE)
1384 {
1385 if (uGstPdpe & X86_PDPE_P)
1386 {
1387 GCPdPt = uGstPdpe & X86_PDPE_PG_MASK;
1388 enmKind = PGMPOOLKIND_PAE_PD_FOR_PAE_PD;
1389 }
1390 else
1391 {
1392 /* PD not present; guest must reload CR3 to change it.
1393 * No need to monitor anything in this case. */
1394 /** @todo r=bird: WTF is hit?!? */
1395 /*Assert(VM_IS_RAW_MODE_ENABLED(pVM)); - ??? */
1396 GCPdPt = uGstPdpe & X86_PDPE_PG_MASK;
1397 enmKind = PGMPOOLKIND_PAE_PD_PHYS;
1398 Assert(uGstPdpe & X86_PDPE_P); /* caller should do this already */
1399 }
1400 }
1401 else
1402 {
1403 GCPdPt = CPUMGetGuestCR3(pVCpu);
1404 enmKind = (PGMPOOLKIND)(PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD + iPdPt);
1405 }
1406
1407 /* Create a reference back to the PDPT by using the index in its shadow page. */
1408 rc = pgmPoolAlloc(pVM, GCPdPt, enmKind, PGMPOOLACCESS_DONTCARE, PGM_A20_IS_ENABLED(pVCpu),
1409 pVCpu->pgm.s.CTX_SUFF(pShwPageCR3)->idx, iPdPt, false /*fLockPage*/,
1410 &pShwPage);
1411 AssertRCReturn(rc, rc);
1412
1413 /* Hook it up. */
1414 ASMAtomicWriteU64(&pPdpe->u, pShwPage->Core.Key | (uGstPdpe & (X86_PDPE_P | X86_PDPE_A)) | (uPdpe & PGM_PDPT_FLAGS));
1415 }
1416 PGM_DYNMAP_UNUSED_HINT(pVCpu, pPdpe);
1417
1418 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1419 return VINF_SUCCESS;
1420}
1421
1422
1423/**
1424 * Gets the pointer to the shadow page directory entry for an address, PAE.
1425 *
1426 * @returns Pointer to the PDE.
1427 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1428 * @param GCPtr The address.
1429 * @param ppShwPde Receives the address of the pgm pool page for the shadow page directory
1430 */
1431DECLINLINE(int) pgmShwGetPaePoolPagePD(PVMCPUCC pVCpu, RTGCPTR GCPtr, PPGMPOOLPAGE *ppShwPde)
1432{
1433 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1434 PGM_LOCK_ASSERT_OWNER(pVM);
1435
1436 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(pVCpu);
1437 AssertReturn(pPdpt, VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT); /* can't happen */
1438 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
1439 X86PGPAEUINT const uPdpe = pPdpt->a[iPdPt].u;
1440 if (!(uPdpe & X86_PDPE_P))
1441 {
1442 LogFlow(("pgmShwGetPaePoolPagePD: PD %d not present (%RX64)\n", iPdPt, uPdpe));
1443 return VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT;
1444 }
1445 AssertMsg(uPdpe & X86_PDPE_PG_MASK, ("GCPtr=%RGv\n", GCPtr));
1446
1447 /* Fetch the pgm pool shadow descriptor. */
1448 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pVM->pgm.s.CTX_SUFF(pPool), uPdpe & X86_PDPE_PG_MASK);
1449 AssertReturn(pShwPde, VERR_PGM_POOL_GET_PAGE_FAILED);
1450
1451 *ppShwPde = pShwPde;
1452 return VINF_SUCCESS;
1453}
1454
1455
1456/**
1457 * Syncs the SHADOW page directory pointer for the specified address.
1458 *
1459 * Allocates backing pages in case the PDPT or PML4 entry is missing.
1460 *
1461 * The caller is responsible for making sure the guest has a valid PD before
1462 * calling this function.
1463 *
1464 * @returns VBox status code.
1465 * @param pVCpu The cross context virtual CPU structure.
1466 * @param GCPtr The address.
1467 * @param uGstPml4e Guest PML4 entry (valid).
1468 * @param uGstPdpe Guest PDPT entry (valid).
1469 * @param ppPD Receives address of page directory
1470 */
1471static int pgmShwSyncLongModePDPtr(PVMCPUCC pVCpu, RTGCPTR64 GCPtr, X86PGPAEUINT uGstPml4e, X86PGPAEUINT uGstPdpe, PX86PDPAE *ppPD)
1472{
1473 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1474 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1475 bool const fNestedPagingOrNoGstPaging = pVM->pgm.s.fNestedPaging || !CPUMIsGuestPagingEnabled(pVCpu);
1476 int rc;
1477
1478 PGM_LOCK_ASSERT_OWNER(pVM);
1479
1480 /*
1481 * PML4.
1482 */
1483 PPGMPOOLPAGE pShwPage;
1484 {
1485 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
1486 PX86PML4E pPml4e = pgmShwGetLongModePML4EPtr(pVCpu, iPml4);
1487 AssertReturn(pPml4e, VERR_PGM_PML4_MAPPING);
1488 X86PGPAEUINT const uPml4e = pPml4e->u;
1489
1490 /* Allocate page directory pointer table if not present. */
1491 if (uPml4e & (X86_PML4E_P | X86_PML4E_PG_MASK))
1492 {
1493 pShwPage = pgmPoolGetPage(pPool, uPml4e & X86_PML4E_PG_MASK);
1494 AssertReturn(pShwPage, VERR_PGM_POOL_GET_PAGE_FAILED);
1495
1496 pgmPoolCacheUsed(pPool, pShwPage);
1497
1498 /* Update the entry if needed. */
1499 X86PGPAEUINT const uPml4eNew = pShwPage->Core.Key | (uGstPml4e & pVCpu->pgm.s.fGstAmd64ShadowedPml4eMask)
1500 | (uPml4e & PGM_PML4_FLAGS);
1501 if (uPml4e == uPml4eNew)
1502 { /* likely */ }
1503 else
1504 ASMAtomicWriteU64(&pPml4e->u, uPml4eNew);
1505 }
1506 else
1507 {
1508 Assert(pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
1509
1510 RTGCPTR64 GCPml4;
1511 PGMPOOLKIND enmKind;
1512 if (fNestedPagingOrNoGstPaging)
1513 {
1514 /* AMD-V nested paging or real/protected mode without paging */
1515 GCPml4 = (RTGCPTR64)iPml4 << X86_PML4_SHIFT; /** @todo bogus calculation for PML5 */
1516 enmKind = PGMPOOLKIND_64BIT_PDPT_FOR_PHYS;
1517 }
1518 else
1519 {
1520 GCPml4 = uGstPml4e & X86_PML4E_PG_MASK;
1521 enmKind = PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT;
1522 }
1523
1524 /* Create a reference back to the PDPT by using the index in its shadow page. */
1525 rc = pgmPoolAlloc(pVM, GCPml4, enmKind, PGMPOOLACCESS_DONTCARE, PGM_A20_IS_ENABLED(pVCpu),
1526 pVCpu->pgm.s.CTX_SUFF(pShwPageCR3)->idx, iPml4, false /*fLockPage*/,
1527 &pShwPage);
1528 AssertRCReturn(rc, rc);
1529
1530 /* Hook it up. */
1531 ASMAtomicWriteU64(&pPml4e->u, pShwPage->Core.Key | (uGstPml4e & pVCpu->pgm.s.fGstAmd64ShadowedPml4eMask)
1532 | (uPml4e & PGM_PML4_FLAGS));
1533 }
1534 }
1535
1536 /*
1537 * PDPT.
1538 */
1539 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
1540 PX86PDPT pPdpt = (PX86PDPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1541 PX86PDPE pPdpe = &pPdpt->a[iPdPt];
1542 X86PGPAEUINT const uPdpe = pPdpe->u;
1543
1544 /* Allocate page directory if not present. */
1545 if (uPdpe & (X86_PDPE_P | X86_PDPE_PG_MASK))
1546 {
1547 pShwPage = pgmPoolGetPage(pPool, uPdpe & X86_PDPE_PG_MASK);
1548 AssertReturn(pShwPage, VERR_PGM_POOL_GET_PAGE_FAILED);
1549
1550 pgmPoolCacheUsed(pPool, pShwPage);
1551
1552 /* Update the entry if needed. */
1553 X86PGPAEUINT const uPdpeNew = pShwPage->Core.Key | (uGstPdpe & pVCpu->pgm.s.fGstAmd64ShadowedPdpeMask)
1554 | (uPdpe & PGM_PDPT_FLAGS);
1555 if (uPdpe == uPdpeNew)
1556 { /* likely */ }
1557 else
1558 ASMAtomicWriteU64(&pPdpe->u, uPdpeNew);
1559 }
1560 else
1561 {
1562 RTGCPTR64 GCPdPt;
1563 PGMPOOLKIND enmKind;
1564 if (fNestedPagingOrNoGstPaging)
1565 {
1566 /* AMD-V nested paging or real/protected mode without paging */
1567 GCPdPt = GCPtr & ~(RT_BIT_64(iPdPt << X86_PDPT_SHIFT) - 1);
1568 enmKind = PGMPOOLKIND_64BIT_PD_FOR_PHYS;
1569 }
1570 else
1571 {
1572 GCPdPt = uGstPdpe & X86_PDPE_PG_MASK;
1573 enmKind = PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD;
1574 }
1575
1576 /* Create a reference back to the PDPT by using the index in its shadow page. */
1577 rc = pgmPoolAlloc(pVM, GCPdPt, enmKind, PGMPOOLACCESS_DONTCARE, PGM_A20_IS_ENABLED(pVCpu),
1578 pShwPage->idx, iPdPt, false /*fLockPage*/,
1579 &pShwPage);
1580 AssertRCReturn(rc, rc);
1581
1582 /* Hook it up. */
1583 ASMAtomicWriteU64(&pPdpe->u,
1584 pShwPage->Core.Key | (uGstPdpe & pVCpu->pgm.s.fGstAmd64ShadowedPdpeMask) | (uPdpe & PGM_PDPT_FLAGS));
1585 }
1586
1587 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1588 return VINF_SUCCESS;
1589}
1590
1591
1592/**
1593 * Gets the SHADOW page directory pointer for the specified address (long mode).
1594 *
1595 * @returns VBox status code.
1596 * @param pVCpu The cross context virtual CPU structure.
1597 * @param GCPtr The address.
1598 * @param ppPml4e Receives the address of the page map level 4 entry.
1599 * @param ppPdpt Receives the address of the page directory pointer table.
1600 * @param ppPD Receives the address of the page directory.
1601 */
1602DECLINLINE(int) pgmShwGetLongModePDPtr(PVMCPUCC pVCpu, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPT *ppPdpt, PX86PDPAE *ppPD)
1603{
1604 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1605 PGM_LOCK_ASSERT_OWNER(pVM);
1606
1607 /*
1608 * PML4
1609 */
1610 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
1611 PCX86PML4E pPml4e = pgmShwGetLongModePML4EPtr(pVCpu, iPml4);
1612 AssertReturn(pPml4e, VERR_PGM_PML4_MAPPING);
1613 if (ppPml4e)
1614 *ppPml4e = (PX86PML4E)pPml4e;
1615 X86PGPAEUINT const uPml4e = pPml4e->u;
1616 Log4(("pgmShwGetLongModePDPtr %RGv (%RHv) %RX64\n", GCPtr, pPml4e, uPml4e));
1617 if (!(uPml4e & X86_PML4E_P)) /** @todo other code is check for NULL page frame number! */
1618 return VERR_PAGE_MAP_LEVEL4_NOT_PRESENT;
1619
1620 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1621 PPGMPOOLPAGE pShwPage = pgmPoolGetPage(pPool, uPml4e & X86_PML4E_PG_MASK);
1622 AssertReturn(pShwPage, VERR_PGM_POOL_GET_PAGE_FAILED);
1623
1624 /*
1625 * PDPT
1626 */
1627 const unsigned iPdPt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
1628 PCX86PDPT pPdpt = *ppPdpt = (PX86PDPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1629 X86PGPAEUINT const uPdpe = pPdpt->a[iPdPt].u;
1630 if (!(uPdpe & X86_PDPE_P)) /** @todo other code is check for NULL page frame number! */
1631 return VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT;
1632
1633 pShwPage = pgmPoolGetPage(pPool, uPdpe & X86_PDPE_PG_MASK);
1634 AssertReturn(pShwPage, VERR_PGM_POOL_GET_PAGE_FAILED);
1635
1636 *ppPD = (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1637 Log4(("pgmShwGetLongModePDPtr %RGv -> *ppPD=%p PDE=%p/%RX64\n", GCPtr, *ppPD, &(*ppPD)->a[(GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK], (*ppPD)->a[(GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK].u));
1638 return VINF_SUCCESS;
1639}
1640
1641
1642/**
1643 * Syncs the SHADOW EPT page directory pointer for the specified address. Allocates
1644 * backing pages in case the PDPT or PML4 entry is missing.
1645 *
1646 * @returns VBox status code.
1647 * @param pVCpu The cross context virtual CPU structure.
1648 * @param GCPtr The address.
1649 * @param ppPdpt Receives address of pdpt
1650 * @param ppPD Receives address of page directory
1651 */
1652static int pgmShwGetEPTPDPtr(PVMCPUCC pVCpu, RTGCPTR64 GCPtr, PEPTPDPT *ppPdpt, PEPTPD *ppPD)
1653{
1654 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1655 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1656 int rc;
1657
1658 Assert(pVM->pgm.s.fNestedPaging);
1659 PGM_LOCK_ASSERT_OWNER(pVM);
1660
1661 /*
1662 * PML4 level.
1663 */
1664 PEPTPML4 pPml4 = (PEPTPML4)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
1665 Assert(pPml4);
1666
1667 /* Allocate page directory pointer table if not present. */
1668 PPGMPOOLPAGE pShwPage;
1669 {
1670 const unsigned iPml4 = (GCPtr >> EPT_PML4_SHIFT) & EPT_PML4_MASK;
1671 PEPTPML4E pPml4e = &pPml4->a[iPml4];
1672 EPTPML4E Pml4e;
1673 Pml4e.u = pPml4e->u;
1674 if (!(Pml4e.u & (EPT_E_PG_MASK | EPT_E_READ)))
1675 {
1676 RTGCPTR64 GCPml4 = (RTGCPTR64)iPml4 << EPT_PML4_SHIFT;
1677 rc = pgmPoolAlloc(pVM, GCPml4, PGMPOOLKIND_EPT_PDPT_FOR_PHYS, PGMPOOLACCESS_DONTCARE, PGM_A20_IS_ENABLED(pVCpu),
1678 pVCpu->pgm.s.CTX_SUFF(pShwPageCR3)->idx, iPml4, false /*fLockPage*/,
1679 &pShwPage);
1680 AssertRCReturn(rc, rc);
1681
1682 /* Hook up the new PDPT now. */
1683 ASMAtomicWriteU64(&pPml4e->u, pShwPage->Core.Key | EPT_E_READ | EPT_E_WRITE | EPT_E_EXECUTE);
1684 }
1685 else
1686 {
1687 pShwPage = pgmPoolGetPage(pPool, pPml4e->u & EPT_PML4E_PG_MASK);
1688 AssertReturn(pShwPage, VERR_PGM_POOL_GET_PAGE_FAILED);
1689
1690 pgmPoolCacheUsed(pPool, pShwPage);
1691
1692 /* Hook up the cached PDPT if needed (probably not given 512*512 PTs to sync). */
1693 if (Pml4e.u == (pShwPage->Core.Key | EPT_E_READ | EPT_E_WRITE | EPT_E_EXECUTE))
1694 { }
1695 else
1696 ASMAtomicWriteU64(&pPml4e->u, pShwPage->Core.Key | EPT_E_READ | EPT_E_WRITE | EPT_E_EXECUTE);
1697 }
1698 }
1699
1700 /*
1701 * PDPT level.
1702 */
1703 const unsigned iPdPt = (GCPtr >> EPT_PDPT_SHIFT) & EPT_PDPT_MASK;
1704 PEPTPDPT pPdpt = (PEPTPDPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1705 PEPTPDPTE pPdpe = &pPdpt->a[iPdPt];
1706
1707 if (ppPdpt)
1708 *ppPdpt = pPdpt;
1709
1710 /* Allocate page directory if not present. */
1711 EPTPDPTE Pdpe;
1712 Pdpe.u = pPdpe->u;
1713 if (!(Pdpe.u & (EPT_E_PG_MASK | EPT_E_READ)))
1714 {
1715 RTGCPTR64 const GCPdPt = GCPtr & ~(RT_BIT_64(EPT_PDPT_SHIFT) - 1);
1716 rc = pgmPoolAlloc(pVM, GCPdPt, PGMPOOLKIND_EPT_PD_FOR_PHYS, PGMPOOLACCESS_DONTCARE, PGM_A20_IS_ENABLED(pVCpu),
1717 pShwPage->idx, iPdPt, false /*fLockPage*/,
1718 &pShwPage);
1719 AssertRCReturn(rc, rc);
1720
1721 /* Hook up the new PD now. */
1722 ASMAtomicWriteU64(&pPdpe->u, pShwPage->Core.Key | EPT_E_READ | EPT_E_WRITE | EPT_E_EXECUTE);
1723 }
1724 else
1725 {
1726 pShwPage = pgmPoolGetPage(pPool, pPdpe->u & EPT_PDPTE_PG_MASK);
1727 AssertReturn(pShwPage, VERR_PGM_POOL_GET_PAGE_FAILED);
1728
1729 pgmPoolCacheUsed(pPool, pShwPage);
1730
1731 /* Hook up the cached PD if needed (probably not given there are 512 PTs we may need sync). */
1732 if (Pdpe.u == (pShwPage->Core.Key | EPT_E_READ | EPT_E_WRITE | EPT_E_EXECUTE))
1733 { }
1734 else
1735 ASMAtomicWriteU64(&pPdpe->u, pShwPage->Core.Key | EPT_E_READ | EPT_E_WRITE | EPT_E_EXECUTE);
1736 }
1737
1738 *ppPD = (PEPTPD)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1739 return VINF_SUCCESS;
1740}
1741
1742
1743#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
1744/**
1745 * Syncs the SHADOW nested-guest page directory pointer for the specified address.
1746 * Allocates backing pages in case the PDPT or PML4 entry is missing.
1747 *
1748 * @returns VBox status code.
1749 * @param pVCpu The cross context virtual CPU structure.
1750 * @param GCPhysNested The nested-guest physical address.
1751 * @param ppPdpt Where to store the PDPT. Optional, can be NULL.
1752 * @param ppPD Where to store the PD. Optional, can be NULL.
1753 * @param pGstWalkAll The guest walk info.
1754 */
1755static int pgmShwGetNestedEPTPDPtr(PVMCPUCC pVCpu, RTGCPTR64 GCPhysNested, PEPTPDPT *ppPdpt, PEPTPD *ppPD,
1756 PPGMPTWALKGST pGstWalkAll)
1757{
1758 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1759 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1760 int rc;
1761
1762 PPGMPOOLPAGE pShwPage;
1763 Assert(pVM->pgm.s.fNestedPaging);
1764 Assert(pVCpu->pgm.s.enmGuestSlatMode == PGMSLAT_EPT);
1765 PGM_LOCK_ASSERT_OWNER(pVM);
1766
1767 /*
1768 * PML4 level.
1769 */
1770 {
1771 PEPTPML4 pPml4 = (PEPTPML4)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
1772 Assert(pPml4);
1773
1774 /* Allocate page directory pointer table if not present. */
1775 {
1776 uint64_t const fShwFlags = pGstWalkAll->u.Ept.Pml4e.u & pVCpu->pgm.s.fGstEptShadowedPml4eMask;
1777 const unsigned iPml4e = (GCPhysNested >> EPT_PML4_SHIFT) & EPT_PML4_MASK;
1778 PEPTPML4E pPml4e = &pPml4->a[iPml4e];
1779
1780 if (!(pPml4e->u & (EPT_E_PG_MASK | EPT_PRESENT_MASK)))
1781 {
1782 RTGCPHYS const GCPhysPdpt = pGstWalkAll->u.Ept.Pml4e.u & EPT_PML4E_PG_MASK;
1783 rc = pgmPoolAlloc(pVM, GCPhysPdpt, PGMPOOLKIND_EPT_PDPT_FOR_EPT_PDPT, PGMPOOLACCESS_DONTCARE,
1784 PGM_A20_IS_ENABLED(pVCpu), pVCpu->pgm.s.CTX_SUFF(pShwPageCR3)->idx, iPml4e, false /*fLockPage*/,
1785 &pShwPage);
1786 AssertRCReturn(rc, rc);
1787
1788 /* Hook up the new PDPT now. */
1789 ASMAtomicWriteU64(&pPml4e->u, pShwPage->Core.Key | fShwFlags);
1790 }
1791 else
1792 {
1793 pShwPage = pgmPoolGetPage(pPool, pPml4e->u & EPT_PML4E_PG_MASK);
1794 AssertReturn(pShwPage, VERR_PGM_POOL_GET_PAGE_FAILED);
1795
1796 pgmPoolCacheUsed(pPool, pShwPage);
1797
1798 /* Hook up the cached PDPT if needed (probably not given 512*512 PTs to sync). */
1799 if (pPml4e->u != (pShwPage->Core.Key | fShwFlags))
1800 ASMAtomicWriteU64(&pPml4e->u, pShwPage->Core.Key | fShwFlags);
1801 }
1802 Assert(PGMPOOL_PAGE_IS_NESTED(pShwPage));
1803 Log7Func(("GstPml4e=%RX64 ShwPml4e=%RX64 iPml4e=%u\n", pGstWalkAll->u.Ept.Pml4e.u, pPml4e->u, iPml4e));
1804 }
1805 }
1806
1807 /*
1808 * PDPT level.
1809 */
1810 {
1811 AssertReturn(!(pGstWalkAll->u.Ept.Pdpte.u & EPT_E_LEAF), VERR_NOT_SUPPORTED); /* shadowing 1GB pages not supported yet. */
1812
1813 PEPTPDPT pPdpt = (PEPTPDPT)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1814 if (ppPdpt)
1815 *ppPdpt = pPdpt;
1816
1817 uint64_t const fShwFlags = pGstWalkAll->u.Ept.Pdpte.u & pVCpu->pgm.s.fGstEptShadowedPdpteMask;
1818 const unsigned iPdPte = (GCPhysNested >> EPT_PDPT_SHIFT) & EPT_PDPT_MASK;
1819 PEPTPDPTE pPdpte = &pPdpt->a[iPdPte];
1820
1821 if (!(pPdpte->u & (EPT_E_PG_MASK | EPT_PRESENT_MASK)))
1822 {
1823 RTGCPHYS const GCPhysPd = pGstWalkAll->u.Ept.Pdpte.u & EPT_PDPTE_PG_MASK;
1824 rc = pgmPoolAlloc(pVM, GCPhysPd, PGMPOOLKIND_EPT_PD_FOR_EPT_PD, PGMPOOLACCESS_DONTCARE, PGM_A20_IS_ENABLED(pVCpu),
1825 pShwPage->idx, iPdPte, false /*fLockPage*/, &pShwPage);
1826 AssertRCReturn(rc, rc);
1827
1828 /* Hook up the new PD now. */
1829 ASMAtomicWriteU64(&pPdpte->u, pShwPage->Core.Key | fShwFlags);
1830 }
1831 else
1832 {
1833 pShwPage = pgmPoolGetPage(pPool, pPdpte->u & EPT_PDPTE_PG_MASK);
1834 AssertReturn(pShwPage, VERR_PGM_POOL_GET_PAGE_FAILED);
1835
1836 pgmPoolCacheUsed(pPool, pShwPage);
1837
1838 /* Hook up the cached PD if needed (probably not given there are 512 PTs we may need sync). */
1839 if (pPdpte->u != (pShwPage->Core.Key | fShwFlags))
1840 ASMAtomicWriteU64(&pPdpte->u, pShwPage->Core.Key | fShwFlags);
1841 }
1842 Assert(PGMPOOL_PAGE_IS_NESTED(pShwPage));
1843 Log7Func(("GstPdpte=%RX64 ShwPdpte=%RX64 iPdPte=%u \n", pGstWalkAll->u.Ept.Pdpte.u, pPdpte->u, iPdPte));
1844
1845 *ppPD = (PEPTPD)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPage);
1846 }
1847
1848 return VINF_SUCCESS;
1849}
1850#endif /* VBOX_WITH_NESTED_HWVIRT_VMX_EPT */
1851
1852
1853#ifdef IN_RING0
1854/**
1855 * Synchronizes a range of nested page table entries.
1856 *
1857 * The caller must own the PGM lock.
1858 *
1859 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1860 * @param GCPhys Where to start.
1861 * @param cPages How many pages which entries should be synced.
1862 * @param enmShwPagingMode The shadow paging mode (PGMMODE_EPT for VT-x,
1863 * host paging mode for AMD-V).
1864 */
1865int pgmShwSyncNestedPageLocked(PVMCPUCC pVCpu, RTGCPHYS GCPhys, uint32_t cPages, PGMMODE enmShwPagingMode)
1866{
1867 PGM_LOCK_ASSERT_OWNER(pVCpu->CTX_SUFF(pVM));
1868
1869/** @todo r=bird: Gotta love this nested paging hacking we're still carrying with us... (Split PGM_TYPE_NESTED.) */
1870 int rc;
1871 switch (enmShwPagingMode)
1872 {
1873 case PGMMODE_32_BIT:
1874 {
1875 X86PDE PdeDummy = { X86_PDE_P | X86_PDE_US | X86_PDE_RW | X86_PDE_A };
1876 rc = PGM_BTH_NAME_32BIT_PROT(SyncPage)(pVCpu, PdeDummy, GCPhys, cPages, ~0U /*uErr*/);
1877 break;
1878 }
1879
1880 case PGMMODE_PAE:
1881 case PGMMODE_PAE_NX:
1882 {
1883 X86PDEPAE PdeDummy = { X86_PDE_P | X86_PDE_US | X86_PDE_RW | X86_PDE_A };
1884 rc = PGM_BTH_NAME_PAE_PROT(SyncPage)(pVCpu, PdeDummy, GCPhys, cPages, ~0U /*uErr*/);
1885 break;
1886 }
1887
1888 case PGMMODE_AMD64:
1889 case PGMMODE_AMD64_NX:
1890 {
1891 X86PDEPAE PdeDummy = { X86_PDE_P | X86_PDE_US | X86_PDE_RW | X86_PDE_A };
1892 rc = PGM_BTH_NAME_AMD64_PROT(SyncPage)(pVCpu, PdeDummy, GCPhys, cPages, ~0U /*uErr*/);
1893 break;
1894 }
1895
1896 case PGMMODE_EPT:
1897 {
1898 X86PDEPAE PdeDummy = { X86_PDE_P | X86_PDE_US | X86_PDE_RW | X86_PDE_A };
1899 rc = PGM_BTH_NAME_EPT_PROT(SyncPage)(pVCpu, PdeDummy, GCPhys, cPages, ~0U /*uErr*/);
1900 break;
1901 }
1902
1903 default:
1904 AssertMsgFailedReturn(("%d\n", enmShwPagingMode), VERR_IPE_NOT_REACHED_DEFAULT_CASE);
1905 }
1906 return rc;
1907}
1908#endif /* IN_RING0 */
1909
1910
1911/**
1912 * Gets effective Guest OS page information.
1913 *
1914 * @returns VBox status code.
1915 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1916 * @param GCPtr Guest Context virtual address of the page.
1917 * @param pWalk Where to store the page walk information.
1918 * @thread EMT(pVCpu)
1919 */
1920VMMDECL(int) PGMGstGetPage(PVMCPUCC pVCpu, RTGCPTR GCPtr, PPGMPTWALK pWalk)
1921{
1922 VMCPU_ASSERT_EMT(pVCpu);
1923 Assert(pWalk);
1924 uintptr_t idx = pVCpu->pgm.s.idxGuestModeData;
1925 AssertReturn(idx < RT_ELEMENTS(g_aPgmGuestModeData), VERR_PGM_MODE_IPE);
1926 AssertReturn(g_aPgmGuestModeData[idx].pfnGetPage, VERR_PGM_MODE_IPE);
1927 return g_aPgmGuestModeData[idx].pfnGetPage(pVCpu, GCPtr, pWalk);
1928}
1929
1930
1931/**
1932 * Gets effective Guest OS page information.
1933 *
1934 * @returns VBox status code.
1935 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1936 * @param GCPtr Guest Context virtual address of the page.
1937 * @param fFlags PGMQPAGE_F_XXX. If zero, no accessed or dirty bits will
1938 * be set.
1939 * @param pWalk Where to store the page walk information.
1940 * @thread EMT(pVCpu)
1941 */
1942VMM_INT_DECL(int) PGMGstQueryPageFast(PVMCPUCC pVCpu, RTGCPTR GCPtr, uint32_t fFlags, PPGMPTWALKFAST pWalk)
1943{
1944 VMCPU_ASSERT_EMT(pVCpu);
1945 Assert(pWalk);
1946 Assert(!(fFlags & ~(PGMQPAGE_F_VALID_MASK)));
1947 Assert(!(fFlags & PGMQPAGE_F_EXECUTE) || !(fFlags & PGMQPAGE_F_WRITE));
1948 uintptr_t idx = pVCpu->pgm.s.idxGuestModeData;
1949 AssertReturn(idx < RT_ELEMENTS(g_aPgmGuestModeData), VERR_PGM_MODE_IPE);
1950 AssertReturn(g_aPgmGuestModeData[idx].pfnGetPage, VERR_PGM_MODE_IPE);
1951 return g_aPgmGuestModeData[idx].pfnQueryPageFast(pVCpu, GCPtr, fFlags, pWalk);
1952}
1953
1954
1955/**
1956 * Maps the guest CR3.
1957 *
1958 * @returns VBox status code.
1959 * @param pVCpu The cross context virtual CPU structure.
1960 * @param GCPhysCr3 The guest CR3 value.
1961 * @param pHCPtrGuestCr3 Where to store the mapped memory.
1962 */
1963DECLINLINE(int) pgmGstMapCr3(PVMCPUCC pVCpu, RTGCPHYS GCPhysCr3, PRTHCPTR pHCPtrGuestCr3)
1964{
1965 /** @todo this needs some reworking wrt. locking? */
1966 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
1967 PGM_LOCK_VOID(pVM);
1968 PPGMPAGE pPageCr3 = pgmPhysGetPage(pVM, GCPhysCr3);
1969 AssertReturnStmt(pPageCr3, PGM_UNLOCK(pVM), VERR_PGM_INVALID_CR3_ADDR);
1970
1971 RTHCPTR HCPtrGuestCr3;
1972 int rc = pgmPhysGCPhys2CCPtrInternalDepr(pVM, pPageCr3, GCPhysCr3, (void **)&HCPtrGuestCr3);
1973 PGM_UNLOCK(pVM);
1974
1975 *pHCPtrGuestCr3 = HCPtrGuestCr3;
1976 return rc;
1977}
1978
1979
1980#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
1981/**
1982 * Unmaps the guest CR3.
1983 *
1984 * @returns VBox status code.
1985 * @param pVCpu The cross context virtual CPU structure.
1986 */
1987DECLINLINE(int) pgmGstUnmapCr3(PVMCPUCC pVCpu)
1988{
1989 uintptr_t const idxBth = pVCpu->pgm.s.idxBothModeData;
1990 AssertReturn(idxBth < RT_ELEMENTS(g_aPgmBothModeData), VERR_PGM_MODE_IPE);
1991 AssertReturn(g_aPgmBothModeData[idxBth].pfnUnmapCR3, VERR_PGM_MODE_IPE);
1992 return g_aPgmBothModeData[idxBth].pfnUnmapCR3(pVCpu);
1993}
1994#endif
1995
1996
1997/**
1998 * Performs a guest page table walk.
1999 *
2000 * The guest should be in paged protect mode or long mode when making a call to
2001 * this function.
2002 *
2003 * @returns VBox status code.
2004 * @retval VINF_SUCCESS on success.
2005 * @retval VERR_PAGE_TABLE_NOT_PRESENT on failure. Check pWalk for details.
2006 * @retval VERR_PGM_NOT_USED_IN_MODE if not paging isn't enabled. @a pWalk is
2007 * not valid, except enmType is PGMPTWALKGSTTYPE_INVALID.
2008 *
2009 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2010 * @param GCPtr The guest virtual address to walk by.
2011 * @param pWalk Where to return the walk result. This is valid for some
2012 * error codes as well.
2013 * @param pGstWalk The guest mode specific page walk information.
2014 */
2015int pgmGstPtWalk(PVMCPUCC pVCpu, RTGCPTR GCPtr, PPGMPTWALK pWalk, PPGMPTWALKGST pGstWalk)
2016{
2017 VMCPU_ASSERT_EMT(pVCpu);
2018 switch (pVCpu->pgm.s.enmGuestMode)
2019 {
2020 case PGMMODE_32_BIT:
2021 pGstWalk->enmType = PGMPTWALKGSTTYPE_32BIT;
2022 return PGM_GST_NAME_32BIT(Walk)(pVCpu, GCPtr, pWalk, &pGstWalk->u.Legacy);
2023
2024 case PGMMODE_PAE:
2025 case PGMMODE_PAE_NX:
2026 pGstWalk->enmType = PGMPTWALKGSTTYPE_PAE;
2027 return PGM_GST_NAME_PAE(Walk)(pVCpu, GCPtr, pWalk, &pGstWalk->u.Pae);
2028
2029 case PGMMODE_AMD64:
2030 case PGMMODE_AMD64_NX:
2031 pGstWalk->enmType = PGMPTWALKGSTTYPE_AMD64;
2032 return PGM_GST_NAME_AMD64(Walk)(pVCpu, GCPtr, pWalk, &pGstWalk->u.Amd64);
2033
2034 case PGMMODE_REAL:
2035 case PGMMODE_PROTECTED:
2036 pGstWalk->enmType = PGMPTWALKGSTTYPE_INVALID;
2037 return VERR_PGM_NOT_USED_IN_MODE;
2038
2039 case PGMMODE_EPT:
2040 case PGMMODE_NESTED_32BIT:
2041 case PGMMODE_NESTED_PAE:
2042 case PGMMODE_NESTED_AMD64:
2043 default:
2044 AssertFailed();
2045 pGstWalk->enmType = PGMPTWALKGSTTYPE_INVALID;
2046 return VERR_PGM_NOT_USED_IN_MODE;
2047 }
2048}
2049
2050
2051#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
2052/**
2053 * Performs a guest second-level address translation (SLAT).
2054 *
2055 * @returns VBox status code.
2056 * @retval VINF_SUCCESS on success.
2057 * @retval VERR_PAGE_TABLE_NOT_PRESENT on failure. Check pWalk for details.
2058 * @retval VERR_PGM_NOT_USED_IN_MODE if not paging isn't enabled. @a pWalk is
2059 * not valid, except enmType is PGMPTWALKGSTTYPE_INVALID.
2060 *
2061 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2062 * @param GCPhysNested The nested-guest physical address being translated.
2063 * @param fIsLinearAddrValid Whether the linear address in @a GCPtrNested is the
2064 * cause for this translation.
2065 * @param GCPtrNested The nested-guest virtual address that initiated the
2066 * SLAT. If none, pass 0 (and not NIL_RTGCPTR).
2067 * @param pWalk Where to return the walk result. This is updated for
2068 * all error codes other than
2069 * VERR_PGM_NOT_USED_IN_MODE.
2070 * @param pGstWalk Where to store the second-level paging-mode specific
2071 * walk info.
2072 */
2073static int pgmGstSlatWalk(PVMCPUCC pVCpu, RTGCPHYS GCPhysNested, bool fIsLinearAddrValid, RTGCPTR GCPtrNested,
2074 PPGMPTWALK pWalk, PPGMPTWALKGST pGstWalk)
2075{
2076 /* SLAT mode must be valid at this point as this should only be used -after- we have determined SLAT mode. */
2077 Assert( pVCpu->pgm.s.enmGuestSlatMode != PGMSLAT_DIRECT
2078 && pVCpu->pgm.s.enmGuestSlatMode != PGMSLAT_INVALID);
2079 AssertPtr(pWalk);
2080 AssertPtr(pGstWalk);
2081 switch (pVCpu->pgm.s.enmGuestSlatMode)
2082 {
2083 case PGMSLAT_EPT:
2084 pGstWalk->enmType = PGMPTWALKGSTTYPE_EPT;
2085 return PGM_GST_SLAT_NAME_EPT(Walk)(pVCpu, GCPhysNested, fIsLinearAddrValid, GCPtrNested, pWalk, &pGstWalk->u.Ept);
2086
2087 default:
2088 AssertFailed();
2089 pGstWalk->enmType = PGMPTWALKGSTTYPE_INVALID;
2090 return VERR_PGM_NOT_USED_IN_MODE;
2091 }
2092}
2093#endif /* VBOX_WITH_NESTED_HWVIRT_VMX_EPT */
2094
2095
2096/**
2097 * Tries to continue the previous walk.
2098 *
2099 * @note Requires the caller to hold the PGM lock from the first
2100 * pgmGstPtWalk() call to the last pgmGstPtWalkNext() call. Otherwise
2101 * we cannot use the pointers.
2102 *
2103 * @returns VBox status code.
2104 * @retval VINF_SUCCESS on success.
2105 * @retval VERR_PAGE_TABLE_NOT_PRESENT on failure. Check pWalk for details.
2106 * @retval VERR_PGM_NOT_USED_IN_MODE if not paging isn't enabled. @a pWalk is
2107 * not valid, except enmType is PGMPTWALKGSTTYPE_INVALID.
2108 *
2109 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2110 * @param GCPtr The guest virtual address to walk by.
2111 * @param pWalk Pointer to the previous walk result and where to return
2112 * the result of this walk. This is valid for some error
2113 * codes as well.
2114 * @param pGstWalk The guest-mode specific walk information.
2115 */
2116int pgmGstPtWalkNext(PVMCPUCC pVCpu, RTGCPTR GCPtr, PPGMPTWALK pWalk, PPGMPTWALKGST pGstWalk)
2117{
2118 /*
2119 * We can only handle successfully walks.
2120 * We also limit ourselves to the next page.
2121 */
2122 if ( pWalk->fSucceeded
2123 && GCPtr - pWalk->GCPtr == GUEST_PAGE_SIZE)
2124 {
2125 Assert(pWalk->uLevel == 0);
2126 if (pGstWalk->enmType == PGMPTWALKGSTTYPE_AMD64)
2127 {
2128 /*
2129 * AMD64
2130 */
2131 if (!pWalk->fGigantPage && !pWalk->fBigPage)
2132 {
2133 /*
2134 * We fall back to full walk if the PDE table changes, if any
2135 * reserved bits are set, or if the effective page access changes.
2136 */
2137 const uint64_t fPteSame = X86_PTE_P | X86_PTE_RW | X86_PTE_US | X86_PTE_PWT
2138 | X86_PTE_PCD | X86_PTE_A | X86_PTE_PAE_NX;
2139 const uint64_t fPdeSame = X86_PDE_P | X86_PDE_RW | X86_PDE_US | X86_PDE_PWT
2140 | X86_PDE_PCD | X86_PDE_A | X86_PDE_PAE_NX | X86_PDE_PS;
2141
2142 if ((GCPtr >> X86_PD_PAE_SHIFT) == (pWalk->GCPtr >> X86_PD_PAE_SHIFT))
2143 {
2144 if (pGstWalk->u.Amd64.pPte)
2145 {
2146 X86PTEPAE Pte;
2147 Pte.u = pGstWalk->u.Amd64.pPte[1].u;
2148 if ( (Pte.u & fPteSame) == (pGstWalk->u.Amd64.Pte.u & fPteSame)
2149 && !(Pte.u & (pVCpu)->pgm.s.fGstAmd64MbzPteMask))
2150 {
2151 pWalk->GCPtr = GCPtr;
2152 pWalk->GCPhys = Pte.u & X86_PTE_PAE_PG_MASK;
2153 pGstWalk->u.Amd64.Pte.u = Pte.u;
2154 pGstWalk->u.Amd64.pPte++;
2155 return VINF_SUCCESS;
2156 }
2157 }
2158 }
2159 else if ((GCPtr >> X86_PDPT_SHIFT) == (pWalk->GCPtr >> X86_PDPT_SHIFT))
2160 {
2161 Assert(!((GCPtr >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK)); /* Must be first PT entry. */
2162 if (pGstWalk->u.Amd64.pPde)
2163 {
2164 X86PDEPAE Pde;
2165 Pde.u = pGstWalk->u.Amd64.pPde[1].u;
2166 if ( (Pde.u & fPdeSame) == (pGstWalk->u.Amd64.Pde.u & fPdeSame)
2167 && !(Pde.u & (pVCpu)->pgm.s.fGstAmd64MbzPdeMask))
2168 {
2169 /* Get the new PTE and check out the first entry. */
2170 int rc = PGM_GCPHYS_2_PTR_BY_VMCPU(pVCpu, PGM_A20_APPLY(pVCpu, (Pde.u & X86_PDE_PAE_PG_MASK)),
2171 &pGstWalk->u.Amd64.pPt);
2172 if (RT_SUCCESS(rc))
2173 {
2174 pGstWalk->u.Amd64.pPte = &pGstWalk->u.Amd64.pPt->a[0];
2175 X86PTEPAE Pte;
2176 Pte.u = pGstWalk->u.Amd64.pPte->u;
2177 if ( (Pte.u & fPteSame) == (pGstWalk->u.Amd64.Pte.u & fPteSame)
2178 && !(Pte.u & (pVCpu)->pgm.s.fGstAmd64MbzPteMask))
2179 {
2180 pWalk->GCPtr = GCPtr;
2181 pWalk->GCPhys = Pte.u & X86_PTE_PAE_PG_MASK;
2182 pGstWalk->u.Amd64.Pte.u = Pte.u;
2183 pGstWalk->u.Amd64.Pde.u = Pde.u;
2184 pGstWalk->u.Amd64.pPde++;
2185 return VINF_SUCCESS;
2186 }
2187 }
2188 }
2189 }
2190 }
2191 }
2192 else if (!pWalk->fGigantPage)
2193 {
2194 if ((GCPtr & X86_PAGE_2M_BASE_MASK) == (pWalk->GCPtr & X86_PAGE_2M_BASE_MASK))
2195 {
2196 pWalk->GCPtr = GCPtr;
2197 pWalk->GCPhys += GUEST_PAGE_SIZE;
2198 return VINF_SUCCESS;
2199 }
2200 }
2201 else
2202 {
2203 if ((GCPtr & X86_PAGE_1G_BASE_MASK) == (pWalk->GCPtr & X86_PAGE_1G_BASE_MASK))
2204 {
2205 pWalk->GCPtr = GCPtr;
2206 pWalk->GCPhys += GUEST_PAGE_SIZE;
2207 return VINF_SUCCESS;
2208 }
2209 }
2210 }
2211 }
2212 /* Case we don't handle. Do full walk. */
2213 return pgmGstPtWalk(pVCpu, GCPtr, pWalk, pGstWalk);
2214}
2215
2216
2217/**
2218 * Modify page flags for a range of pages in the guest's tables
2219 *
2220 * The existing flags are ANDed with the fMask and ORed with the fFlags.
2221 *
2222 * @returns VBox status code.
2223 * @param pVCpu The cross context virtual CPU structure.
2224 * @param GCPtr Virtual address of the first page in the range.
2225 * @param cb Size (in bytes) of the range to apply the modification to.
2226 * @param fFlags The OR mask - page flags X86_PTE_*, excluding the page mask of course.
2227 * @param fMask The AND mask - page flags X86_PTE_*, excluding the page mask of course.
2228 * Be very CAREFUL when ~'ing constants which could be 32-bit!
2229 */
2230VMMDECL(int) PGMGstModifyPage(PVMCPUCC pVCpu, RTGCPTR GCPtr, size_t cb, uint64_t fFlags, uint64_t fMask)
2231{
2232 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,GstModifyPage), a);
2233 VMCPU_ASSERT_EMT(pVCpu);
2234
2235 /*
2236 * Validate input.
2237 */
2238 AssertMsg(!(fFlags & X86_PTE_PAE_PG_MASK), ("fFlags=%#llx\n", fFlags));
2239 Assert(cb);
2240
2241 LogFlow(("PGMGstModifyPage %RGv %d bytes fFlags=%08llx fMask=%08llx\n", GCPtr, cb, fFlags, fMask));
2242
2243 /*
2244 * Adjust input.
2245 */
2246 cb += GCPtr & GUEST_PAGE_OFFSET_MASK;
2247 cb = RT_ALIGN_Z(cb, GUEST_PAGE_SIZE);
2248 GCPtr &= ~(RTGCPTR)GUEST_PAGE_OFFSET_MASK;
2249
2250 /*
2251 * Call worker.
2252 */
2253 uintptr_t idx = pVCpu->pgm.s.idxGuestModeData;
2254 AssertReturn(idx < RT_ELEMENTS(g_aPgmGuestModeData), VERR_PGM_MODE_IPE);
2255 AssertReturn(g_aPgmGuestModeData[idx].pfnModifyPage, VERR_PGM_MODE_IPE);
2256 int rc = g_aPgmGuestModeData[idx].pfnModifyPage(pVCpu, GCPtr, cb, fFlags, fMask);
2257
2258 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,GstModifyPage), a);
2259 return rc;
2260}
2261
2262
2263/**
2264 * Checks whether the given PAE PDPEs are potentially valid for the guest.
2265 *
2266 * @returns @c true if the PDPE is valid, @c false otherwise.
2267 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2268 * @param paPaePdpes The PAE PDPEs to validate.
2269 *
2270 * @remarks This function -only- checks the reserved bits in the PDPE entries.
2271 */
2272VMM_INT_DECL(bool) PGMGstArePaePdpesValid(PVMCPUCC pVCpu, PCX86PDPE paPaePdpes)
2273{
2274 Assert(paPaePdpes);
2275 for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
2276 {
2277 X86PDPE const PaePdpe = paPaePdpes[i];
2278 if ( !(PaePdpe.u & X86_PDPE_P)
2279 || !(PaePdpe.u & pVCpu->pgm.s.fGstPaeMbzPdpeMask))
2280 { /* likely */ }
2281 else
2282 return false;
2283 }
2284 return true;
2285}
2286
2287
2288/**
2289 * Performs the lazy mapping of the 32-bit guest PD.
2290 *
2291 * @returns VBox status code.
2292 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2293 * @param ppPd Where to return the pointer to the mapping. This is
2294 * always set.
2295 */
2296int pgmGstLazyMap32BitPD(PVMCPUCC pVCpu, PX86PD *ppPd)
2297{
2298 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2299 PGM_LOCK_VOID(pVM);
2300
2301 Assert(!pVCpu->pgm.s.CTX_SUFF(pGst32BitPd));
2302
2303 RTGCPHYS GCPhysCR3 = pgmGetGuestMaskedCr3(pVCpu, pVCpu->pgm.s.GCPhysCR3);
2304 PPGMPAGE pPage;
2305 int rc = pgmPhysGetPageEx(pVM, GCPhysCR3, &pPage);
2306 if (RT_SUCCESS(rc))
2307 {
2308 rc = pgmPhysGCPhys2CCPtrInternalDepr(pVM, pPage, GCPhysCR3, (void **)ppPd);
2309 if (RT_SUCCESS(rc))
2310 {
2311# ifdef IN_RING3
2312 pVCpu->pgm.s.pGst32BitPdR0 = NIL_RTR0PTR;
2313 pVCpu->pgm.s.pGst32BitPdR3 = *ppPd;
2314# else
2315 pVCpu->pgm.s.pGst32BitPdR3 = NIL_RTR0PTR;
2316 pVCpu->pgm.s.pGst32BitPdR0 = *ppPd;
2317# endif
2318 PGM_UNLOCK(pVM);
2319 return VINF_SUCCESS;
2320 }
2321 AssertRC(rc);
2322 }
2323 PGM_UNLOCK(pVM);
2324
2325 *ppPd = NULL;
2326 return rc;
2327}
2328
2329
2330/**
2331 * Performs the lazy mapping of the PAE guest PDPT.
2332 *
2333 * @returns VBox status code.
2334 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2335 * @param ppPdpt Where to return the pointer to the mapping. This is
2336 * always set.
2337 */
2338int pgmGstLazyMapPaePDPT(PVMCPUCC pVCpu, PX86PDPT *ppPdpt)
2339{
2340 Assert(!pVCpu->pgm.s.CTX_SUFF(pGstPaePdpt));
2341 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2342 PGM_LOCK_VOID(pVM);
2343
2344 RTGCPHYS GCPhysCR3 = pgmGetGuestMaskedCr3(pVCpu, pVCpu->pgm.s.GCPhysCR3);
2345 PPGMPAGE pPage;
2346 int rc = pgmPhysGetPageEx(pVM, GCPhysCR3, &pPage);
2347 if (RT_SUCCESS(rc))
2348 {
2349 rc = pgmPhysGCPhys2CCPtrInternalDepr(pVM, pPage, GCPhysCR3, (void **)ppPdpt);
2350 if (RT_SUCCESS(rc))
2351 {
2352# ifdef IN_RING3
2353 pVCpu->pgm.s.pGstPaePdptR0 = NIL_RTR0PTR;
2354 pVCpu->pgm.s.pGstPaePdptR3 = *ppPdpt;
2355# else
2356 pVCpu->pgm.s.pGstPaePdptR3 = NIL_RTR3PTR;
2357 pVCpu->pgm.s.pGstPaePdptR0 = *ppPdpt;
2358# endif
2359 PGM_UNLOCK(pVM);
2360 return VINF_SUCCESS;
2361 }
2362 AssertRC(rc);
2363 }
2364
2365 PGM_UNLOCK(pVM);
2366 *ppPdpt = NULL;
2367 return rc;
2368}
2369
2370
2371/**
2372 * Performs the lazy mapping / updating of a PAE guest PD.
2373 *
2374 * @returns Pointer to the mapping.
2375 * @returns VBox status code.
2376 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2377 * @param iPdpt Which PD entry to map (0..3).
2378 * @param ppPd Where to return the pointer to the mapping. This is
2379 * always set.
2380 */
2381int pgmGstLazyMapPaePD(PVMCPUCC pVCpu, uint32_t iPdpt, PX86PDPAE *ppPd)
2382{
2383 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2384 PGM_LOCK_VOID(pVM);
2385
2386 PX86PDPT pGuestPDPT = pVCpu->pgm.s.CTX_SUFF(pGstPaePdpt);
2387 Assert(pGuestPDPT);
2388 Assert(pGuestPDPT->a[iPdpt].u & X86_PDPE_P);
2389 RTGCPHYS GCPhys = pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK;
2390 bool const fChanged = pVCpu->pgm.s.aGCPhysGstPaePDs[iPdpt] != GCPhys;
2391
2392 PPGMPAGE pPage;
2393 int rc = pgmPhysGetPageEx(pVM, GCPhys, &pPage);
2394 if (RT_SUCCESS(rc))
2395 {
2396 rc = pgmPhysGCPhys2CCPtrInternalDepr(pVM, pPage, GCPhys, (void **)ppPd);
2397 AssertRC(rc);
2398 if (RT_SUCCESS(rc))
2399 {
2400# ifdef IN_RING3
2401 pVCpu->pgm.s.apGstPaePDsR0[iPdpt] = NIL_RTR0PTR;
2402 pVCpu->pgm.s.apGstPaePDsR3[iPdpt] = *ppPd;
2403# else
2404 pVCpu->pgm.s.apGstPaePDsR3[iPdpt] = NIL_RTR3PTR;
2405 pVCpu->pgm.s.apGstPaePDsR0[iPdpt] = *ppPd;
2406# endif
2407 if (fChanged)
2408 pVCpu->pgm.s.aGCPhysGstPaePDs[iPdpt] = GCPhys;
2409 PGM_UNLOCK(pVM);
2410 return VINF_SUCCESS;
2411 }
2412 }
2413
2414 /* Invalid page or some failure, invalidate the entry. */
2415 pVCpu->pgm.s.aGCPhysGstPaePDs[iPdpt] = NIL_RTGCPHYS;
2416 pVCpu->pgm.s.apGstPaePDsR3[iPdpt] = NIL_RTR3PTR;
2417 pVCpu->pgm.s.apGstPaePDsR0[iPdpt] = NIL_RTR0PTR;
2418
2419 PGM_UNLOCK(pVM);
2420 return rc;
2421}
2422
2423
2424/**
2425 * Performs the lazy mapping of the 32-bit guest PD.
2426 *
2427 * @returns VBox status code.
2428 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2429 * @param ppPml4 Where to return the pointer to the mapping. This will
2430 * always be set.
2431 */
2432int pgmGstLazyMapPml4(PVMCPUCC pVCpu, PX86PML4 *ppPml4)
2433{
2434 Assert(!pVCpu->pgm.s.CTX_SUFF(pGstAmd64Pml4));
2435 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2436 PGM_LOCK_VOID(pVM);
2437
2438 RTGCPHYS GCPhysCR3 = pgmGetGuestMaskedCr3(pVCpu, pVCpu->pgm.s.GCPhysCR3);
2439 PPGMPAGE pPage;
2440 int rc = pgmPhysGetPageEx(pVM, GCPhysCR3, &pPage);
2441 if (RT_SUCCESS(rc))
2442 {
2443 rc = pgmPhysGCPhys2CCPtrInternalDepr(pVM, pPage, GCPhysCR3, (void **)ppPml4);
2444 if (RT_SUCCESS(rc))
2445 {
2446# ifdef IN_RING3
2447 pVCpu->pgm.s.pGstAmd64Pml4R0 = NIL_RTR0PTR;
2448 pVCpu->pgm.s.pGstAmd64Pml4R3 = *ppPml4;
2449# else
2450 pVCpu->pgm.s.pGstAmd64Pml4R3 = NIL_RTR3PTR;
2451 pVCpu->pgm.s.pGstAmd64Pml4R0 = *ppPml4;
2452# endif
2453 PGM_UNLOCK(pVM);
2454 return VINF_SUCCESS;
2455 }
2456 }
2457
2458 PGM_UNLOCK(pVM);
2459 *ppPml4 = NULL;
2460 return rc;
2461}
2462
2463
2464#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
2465 /**
2466 * Performs the lazy mapping of the guest PML4 table when using EPT paging.
2467 *
2468 * @returns VBox status code.
2469 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2470 * @param ppEptPml4 Where to return the pointer to the mapping. This will
2471 * always be set.
2472 */
2473int pgmGstLazyMapEptPml4(PVMCPUCC pVCpu, PEPTPML4 *ppEptPml4)
2474{
2475 Assert(!pVCpu->pgm.s.CTX_SUFF(pGstEptPml4));
2476 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2477 PGM_LOCK_VOID(pVM);
2478
2479 RTGCPHYS const GCPhysEpt = pVCpu->pgm.s.uEptPtr & EPT_EPTP_PG_MASK;
2480 PPGMPAGE pPage;
2481 int rc = pgmPhysGetPageEx(pVM, GCPhysEpt, &pPage);
2482 if (RT_SUCCESS(rc))
2483 {
2484 rc = pgmPhysGCPhys2CCPtrInternalDepr(pVM, pPage, GCPhysEpt, (void **)ppEptPml4);
2485 if (RT_SUCCESS(rc))
2486 {
2487# ifdef IN_RING3
2488 pVCpu->pgm.s.pGstEptPml4R0 = NIL_RTR0PTR;
2489 pVCpu->pgm.s.pGstEptPml4R3 = *ppEptPml4;
2490# else
2491 pVCpu->pgm.s.pGstEptPml4R3 = NIL_RTR3PTR;
2492 pVCpu->pgm.s.pGstEptPml4R0 = *ppEptPml4;
2493# endif
2494 PGM_UNLOCK(pVM);
2495 return VINF_SUCCESS;
2496 }
2497 }
2498
2499 PGM_UNLOCK(pVM);
2500 *ppEptPml4 = NULL;
2501 return rc;
2502}
2503#endif
2504
2505
2506/**
2507 * Gets the current CR3 register value for the shadow memory context.
2508 * @returns CR3 value.
2509 * @param pVCpu The cross context virtual CPU structure.
2510 */
2511VMMDECL(RTHCPHYS) PGMGetHyperCR3(PVMCPU pVCpu)
2512{
2513 PPGMPOOLPAGE pPoolPage = pVCpu->pgm.s.CTX_SUFF(pShwPageCR3);
2514 AssertPtrReturn(pPoolPage, NIL_RTHCPHYS);
2515 return pPoolPage->Core.Key;
2516}
2517
2518
2519/**
2520 * Forces lazy remapping of the guest's PAE page-directory structures.
2521 *
2522 * @param pVCpu The cross context virtual CPU structure.
2523 */
2524static void pgmGstFlushPaePdpes(PVMCPU pVCpu)
2525{
2526 for (unsigned i = 0; i < RT_ELEMENTS(pVCpu->pgm.s.aGCPhysGstPaePDs); i++)
2527 {
2528 pVCpu->pgm.s.apGstPaePDsR3[i] = 0;
2529 pVCpu->pgm.s.apGstPaePDsR0[i] = 0;
2530 pVCpu->pgm.s.aGCPhysGstPaePDs[i] = NIL_RTGCPHYS;
2531 }
2532}
2533
2534
2535#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
2536/**
2537 * Performs second-level address translation for the given CR3 and updates the
2538 * nested-guest CR3 when successful.
2539 *
2540 * @returns VBox status code.
2541 * @param pVCpu The cross context virtual CPU structure.
2542 * @param uCr3 The masked nested-guest CR3 value.
2543 * @param pGCPhysCR3 Where to store the translated CR3.
2544 *
2545 * @warning This updates PGMCPU::GCPhysNstGstCR3 when the translation succeeds. Be
2546 * mindful of this in code that's hyper sensitive to the order of
2547 * operations.
2548 */
2549static int pgmGstSlatTranslateCr3(PVMCPUCC pVCpu, uint64_t uCr3, PRTGCPHYS pGCPhysCr3)
2550{
2551 if (uCr3 != pVCpu->pgm.s.GCPhysNstGstCR3)
2552 {
2553 PGMPTWALK Walk;
2554 PGMPTWALKGST GstWalk;
2555 int const rc = pgmGstSlatWalk(pVCpu, uCr3, false /* fIsLinearAddrValid */, 0 /* GCPtrNested */, &Walk, &GstWalk);
2556 if (RT_SUCCESS(rc))
2557 {
2558 /* Update nested-guest CR3. */
2559 pVCpu->pgm.s.GCPhysNstGstCR3 = uCr3;
2560
2561 /* Pass back the translated result. */
2562 *pGCPhysCr3 = Walk.GCPhys;
2563 return VINF_SUCCESS;
2564 }
2565
2566 /* Translation failed. */
2567 *pGCPhysCr3 = NIL_RTGCPHYS;
2568 return rc;
2569 }
2570
2571 /*
2572 * If the nested-guest CR3 has not changed, then the previously
2573 * translated CR3 result (i.e. GCPhysCR3) is passed back.
2574 */
2575 *pGCPhysCr3 = pVCpu->pgm.s.GCPhysCR3;
2576 return VINF_SUCCESS;
2577}
2578#endif
2579
2580
2581/**
2582 * Performs and schedules necessary updates following a CR3 load or reload.
2583 *
2584 * This will normally involve mapping the guest PD or nPDPT
2585 *
2586 * @returns VBox status code.
2587 * @retval VINF_PGM_SYNC_CR3 if monitoring requires a CR3 sync. This can
2588 * safely be ignored and overridden since the FF will be set too then.
2589 * @param pVCpu The cross context virtual CPU structure.
2590 * @param cr3 The new cr3.
2591 * @param fGlobal Indicates whether this is a global flush or not.
2592 */
2593VMMDECL(int) PGMFlushTLB(PVMCPUCC pVCpu, uint64_t cr3, bool fGlobal)
2594{
2595 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,FlushTLB), a);
2596 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2597
2598 VMCPU_ASSERT_EMT(pVCpu);
2599
2600 /*
2601 * Always flag the necessary updates; necessary for hardware acceleration
2602 */
2603 /** @todo optimize this, it shouldn't always be necessary. */
2604 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL);
2605 if (fGlobal)
2606 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
2607
2608 /*
2609 * Remap the CR3 content and adjust the monitoring if CR3 was actually changed.
2610 */
2611 RTGCPHYS const GCPhysOldCR3 = pVCpu->pgm.s.GCPhysCR3;
2612 RTGCPHYS GCPhysCR3 = pgmGetGuestMaskedCr3(pVCpu, cr3);
2613#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
2614 if ( pVCpu->pgm.s.enmGuestSlatMode == PGMSLAT_EPT
2615 && PGMMODE_WITH_PAGING(pVCpu->pgm.s.enmGuestMode))
2616 {
2617 RTGCPHYS GCPhysOut;
2618 int const rc = pgmGstSlatTranslateCr3(pVCpu, GCPhysCR3, &GCPhysOut);
2619 if (RT_SUCCESS(rc))
2620 GCPhysCR3 = GCPhysOut;
2621 else
2622 {
2623 /* CR3 SLAT translation failed but we try to pretend it
2624 succeeded for the reasons mentioned in PGMHCChangeMode(). */
2625 AssertMsgFailed(("SLAT failed for CR3 %#RX64 rc=%Rrc\n", cr3, rc));
2626 int const rc2 = pgmGstUnmapCr3(pVCpu);
2627 pVCpu->pgm.s.GCPhysCR3 = NIL_RTGCPHYS;
2628 pVCpu->pgm.s.GCPhysNstGstCR3 = NIL_RTGCPHYS;
2629 return rc2;
2630 }
2631 }
2632#endif
2633
2634 LogFlowFunc(("cr3=%RX64 old=%RX64 fGlobal=%d\n", cr3, GCPhysOldCR3, fGlobal));
2635 int rc = VINF_SUCCESS;
2636 if (GCPhysOldCR3 != GCPhysCR3)
2637 {
2638 uintptr_t const idxBth = pVCpu->pgm.s.idxBothModeData;
2639 AssertReturn(idxBth < RT_ELEMENTS(g_aPgmBothModeData), VERR_PGM_MODE_IPE);
2640 AssertReturn(g_aPgmBothModeData[idxBth].pfnMapCR3, VERR_PGM_MODE_IPE);
2641
2642 pVCpu->pgm.s.GCPhysCR3 = GCPhysCR3;
2643 rc = g_aPgmBothModeData[idxBth].pfnMapCR3(pVCpu, GCPhysCR3);
2644 if (RT_LIKELY(rc == VINF_SUCCESS))
2645 { }
2646 else
2647 {
2648 AssertMsg(rc == VINF_PGM_SYNC_CR3, ("%Rrc\n", rc));
2649 Assert(VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL | VMCPU_FF_PGM_SYNC_CR3));
2650 pVCpu->pgm.s.CTX_SUFF(fPaePdpesAndCr3Mapped) = false;
2651 pVCpu->pgm.s.GCPhysPaeCR3 = NIL_RTGCPHYS;
2652 pVCpu->pgm.s.GCPhysCR3 = GCPhysOldCR3;
2653 pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_MAP_CR3;
2654 }
2655
2656 if (fGlobal)
2657 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,FlushTLBNewCR3Global));
2658 else
2659 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,FlushTLBNewCR3));
2660 }
2661 else
2662 {
2663#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
2664 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2665 if (pPool->cDirtyPages)
2666 {
2667 PGM_LOCK_VOID(pVM);
2668 pgmPoolResetDirtyPages(pVM);
2669 PGM_UNLOCK(pVM);
2670 }
2671#endif
2672 if (fGlobal)
2673 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,FlushTLBSameCR3Global));
2674 else
2675 STAM_COUNTER_INC(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,FlushTLBSameCR3));
2676
2677 /*
2678 * Flush PAE PDPTEs.
2679 */
2680 if (PGMMODE_IS_PAE(pVCpu->pgm.s.enmGuestMode))
2681 pgmGstFlushPaePdpes(pVCpu);
2682 }
2683
2684 if (!fGlobal)
2685 IEMTlbInvalidateAll(pVCpu);
2686 else
2687 IEMTlbInvalidateAllGlobal(pVCpu);
2688 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,FlushTLB), a);
2689 return rc;
2690}
2691
2692
2693/**
2694 * Performs and schedules necessary updates following a CR3 load or reload when
2695 * using nested or extended paging.
2696 *
2697 * This API is an alternative to PGMFlushTLB that avoids actually flushing the
2698 * TLB and triggering a SyncCR3.
2699 *
2700 * This will normally involve mapping the guest PD or nPDPT
2701 *
2702 * @returns VBox status code.
2703 * @retval VINF_SUCCESS.
2704 * @retval VINF_PGM_SYNC_CR3 if monitoring requires a CR3 sync (not for nested
2705 * paging modes). This can safely be ignored and overridden since the
2706 * FF will be set too then.
2707 * @param pVCpu The cross context virtual CPU structure.
2708 * @param cr3 The new CR3.
2709 */
2710VMMDECL(int) PGMUpdateCR3(PVMCPUCC pVCpu, uint64_t cr3)
2711{
2712 VMCPU_ASSERT_EMT(pVCpu);
2713
2714 /* We assume we're only called in nested paging mode. */
2715 Assert(pVCpu->CTX_SUFF(pVM)->pgm.s.fNestedPaging || pVCpu->pgm.s.enmShadowMode == PGMMODE_EPT);
2716
2717 /*
2718 * Remap the CR3 content and adjust the monitoring if CR3 was actually changed.
2719 */
2720 RTGCPHYS const GCPhysOldCR3 = pVCpu->pgm.s.GCPhysCR3;
2721 RTGCPHYS GCPhysCR3 = pgmGetGuestMaskedCr3(pVCpu, cr3);
2722#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
2723 if (pVCpu->pgm.s.enmGuestSlatMode == PGMSLAT_EPT)
2724 {
2725 RTGCPHYS GCPhysOut;
2726 int const rc = pgmGstSlatTranslateCr3(pVCpu, GCPhysCR3, &GCPhysOut);
2727 if (RT_SUCCESS(rc))
2728 GCPhysCR3 = GCPhysOut;
2729 else
2730 {
2731 /* CR3 SLAT translation failed but we try to pretend it
2732 succeeded for the reasons mentioned in PGMHCChangeMode(). */
2733 Log(("SLAT failed for CR3 %#RX64 rc=%Rrc\n", cr3, rc));
2734 int const rc2 = pgmGstUnmapCr3(pVCpu);
2735 pVCpu->pgm.s.GCPhysCR3 = NIL_RTGCPHYS;
2736 pVCpu->pgm.s.GCPhysNstGstCR3 = NIL_RTGCPHYS;
2737 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_HM_UPDATE_CR3);
2738 return rc2;
2739 }
2740 }
2741#endif
2742
2743 LogFlowFunc(("cr3=%RX64 old=%RX64\n", cr3, GCPhysOldCR3));
2744 int rc = VINF_SUCCESS;
2745 if (GCPhysOldCR3 != GCPhysCR3)
2746 {
2747 uintptr_t const idxBth = pVCpu->pgm.s.idxBothModeData;
2748 AssertReturn(idxBth < RT_ELEMENTS(g_aPgmBothModeData), VERR_PGM_MODE_IPE);
2749 AssertReturn(g_aPgmBothModeData[idxBth].pfnMapCR3, VERR_PGM_MODE_IPE);
2750
2751 pVCpu->pgm.s.GCPhysCR3 = GCPhysCR3;
2752 rc = g_aPgmBothModeData[idxBth].pfnMapCR3(pVCpu, GCPhysCR3);
2753
2754 AssertRCSuccess(rc); /* Assumes VINF_PGM_SYNC_CR3 doesn't apply to nested paging. */ /** @todo this isn't true for the mac, but we need hw to test/fix this. */
2755 }
2756 /*
2757 * Flush PAE PDPTEs.
2758 */
2759 else if (PGMMODE_IS_PAE(pVCpu->pgm.s.enmGuestMode))
2760 pgmGstFlushPaePdpes(pVCpu);
2761
2762 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_HM_UPDATE_CR3);
2763 return rc;
2764}
2765
2766
2767/**
2768 * Synchronize the paging structures.
2769 *
2770 * This function is called in response to the VM_FF_PGM_SYNC_CR3 and
2771 * VM_FF_PGM_SYNC_CR3_NONGLOBAL. Those two force action flags are set
2772 * in several places, most importantly whenever the CR3 is loaded.
2773 *
2774 * @returns VBox status code. May return VINF_PGM_SYNC_CR3 in RC/R0.
2775 * @retval VERR_PGM_NO_HYPERVISOR_ADDRESS in raw-mode when we're unable to map
2776 * the VMM into guest context.
2777 * @param pVCpu The cross context virtual CPU structure.
2778 * @param cr0 Guest context CR0 register
2779 * @param cr3 Guest context CR3 register
2780 * @param cr4 Guest context CR4 register
2781 * @param fGlobal Including global page directories or not
2782 */
2783VMMDECL(int) PGMSyncCR3(PVMCPUCC pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal)
2784{
2785 int rc;
2786
2787 VMCPU_ASSERT_EMT(pVCpu);
2788
2789 /*
2790 * The pool may have pending stuff and even require a return to ring-3 to
2791 * clear the whole thing.
2792 */
2793 rc = pgmPoolSyncCR3(pVCpu);
2794 if (rc != VINF_SUCCESS)
2795 return rc;
2796
2797 /*
2798 * We might be called when we shouldn't.
2799 *
2800 * The mode switching will ensure that the PD is resynced after every mode
2801 * switch. So, if we find ourselves here when in protected or real mode
2802 * we can safely clear the FF and return immediately.
2803 */
2804 if (pVCpu->pgm.s.enmGuestMode <= PGMMODE_PROTECTED)
2805 {
2806 Assert((cr0 & (X86_CR0_PG | X86_CR0_PE)) != (X86_CR0_PG | X86_CR0_PE));
2807 Assert(!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL));
2808 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
2809 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL);
2810 return VINF_SUCCESS;
2811 }
2812
2813 /* If global pages are not supported, then all flushes are global. */
2814 if (!(cr4 & X86_CR4_PGE))
2815 fGlobal = true;
2816 LogFlow(("PGMSyncCR3: cr0=%RX64 cr3=%RX64 cr4=%RX64 fGlobal=%d[%d,%d]\n", cr0, cr3, cr4, fGlobal,
2817 VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3), VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)));
2818
2819 /*
2820 * Check if we need to finish an aborted MapCR3 call (see PGMFlushTLB).
2821 * This should be done before SyncCR3.
2822 */
2823 if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_MAP_CR3)
2824 {
2825 pVCpu->pgm.s.fSyncFlags &= ~PGM_SYNC_MAP_CR3;
2826
2827 RTGCPHYS const GCPhysOldCR3 = pVCpu->pgm.s.GCPhysCR3;
2828 RTGCPHYS GCPhysCR3 = pgmGetGuestMaskedCr3(pVCpu, cr3);
2829#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
2830 if (pVCpu->pgm.s.enmGuestSlatMode == PGMSLAT_EPT)
2831 {
2832 RTGCPHYS GCPhysOut;
2833 int rc2 = pgmGstSlatTranslateCr3(pVCpu, GCPhysCR3, &GCPhysOut);
2834 if (RT_SUCCESS(rc2))
2835 GCPhysCR3 = GCPhysOut;
2836 else
2837 {
2838 /* CR3 SLAT translation failed but we try to pretend it
2839 succeeded for the reasons mentioned in PGMHCChangeMode(). */
2840 AssertMsgFailed(("Failed to translate CR3 %#RX64. rc=%Rrc\n", cr3, rc2));
2841 pVCpu->pgm.s.GCPhysCR3 = NIL_RTGCPHYS;
2842 pVCpu->pgm.s.GCPhysNstGstCR3 = NIL_RTGCPHYS;
2843 return rc2;
2844 }
2845 }
2846#endif
2847 Assert(!pVCpu->pgm.s.CTX_SUFF(fPaePdpesAndCr3Mapped));
2848 if (GCPhysOldCR3 != GCPhysCR3)
2849 {
2850 uintptr_t const idxBth = pVCpu->pgm.s.idxBothModeData;
2851 AssertReturn(idxBth < RT_ELEMENTS(g_aPgmBothModeData), VERR_PGM_MODE_IPE);
2852 AssertReturn(g_aPgmBothModeData[idxBth].pfnMapCR3, VERR_PGM_MODE_IPE);
2853 pVCpu->pgm.s.GCPhysCR3 = GCPhysCR3;
2854 rc = g_aPgmBothModeData[idxBth].pfnMapCR3(pVCpu, GCPhysCR3);
2855 }
2856
2857 /* Make sure we check for pending pgm pool syncs as we clear VMCPU_FF_PGM_SYNC_CR3 later on! */
2858 if ( rc == VINF_PGM_SYNC_CR3
2859 || (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL))
2860 {
2861 Log(("PGMSyncCR3: pending pgm pool sync after MapCR3!\n"));
2862#ifdef IN_RING3
2863 rc = pgmPoolSyncCR3(pVCpu);
2864#else
2865 if (rc == VINF_PGM_SYNC_CR3)
2866 pVCpu->pgm.s.GCPhysCR3 = GCPhysOldCR3;
2867 return VINF_PGM_SYNC_CR3;
2868#endif
2869 }
2870 AssertRCReturn(rc, rc);
2871 AssertRCSuccessReturn(rc, VERR_IPE_UNEXPECTED_INFO_STATUS);
2872 }
2873
2874 /*
2875 * Let the 'Bth' function do the work and we'll just keep track of the flags.
2876 */
2877 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncCR3), a);
2878
2879 uintptr_t const idxBth = pVCpu->pgm.s.idxBothModeData;
2880 AssertReturn(idxBth < RT_ELEMENTS(g_aPgmBothModeData), VERR_PGM_MODE_IPE);
2881 AssertReturn(g_aPgmBothModeData[idxBth].pfnSyncCR3, VERR_PGM_MODE_IPE);
2882 rc = g_aPgmBothModeData[idxBth].pfnSyncCR3(pVCpu, cr0, cr3, cr4, fGlobal);
2883
2884 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncCR3), a);
2885 AssertMsg(rc == VINF_SUCCESS || rc == VINF_PGM_SYNC_CR3 || RT_FAILURE(rc), ("rc=%Rrc\n", rc));
2886 if (rc == VINF_SUCCESS)
2887 {
2888 if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
2889 {
2890 /* Go back to ring 3 if a pgm pool sync is again pending. */
2891 return VINF_PGM_SYNC_CR3;
2892 }
2893
2894 if (!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_ALWAYS))
2895 {
2896 Assert(!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL));
2897 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
2898 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL);
2899 }
2900 }
2901
2902 /*
2903 * Now flush the CR3 (guest context).
2904 */
2905 if (rc == VINF_SUCCESS)
2906 PGM_INVL_VCPU_TLBS(pVCpu);
2907 return rc;
2908}
2909
2910
2911/**
2912 * Maps all the PAE PDPE entries.
2913 *
2914 * @returns VBox status code.
2915 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2916 * @param paPaePdpes The new PAE PDPE values.
2917 *
2918 * @remarks This function may be invoked during the process of changing the guest
2919 * paging mode to PAE, hence the guest state (CR0, CR4 etc.) may not
2920 * reflect PAE paging just yet.
2921 */
2922VMM_INT_DECL(int) PGMGstMapPaePdpes(PVMCPUCC pVCpu, PCX86PDPE paPaePdpes)
2923{
2924 Assert(paPaePdpes);
2925 for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
2926 {
2927 X86PDPE const PaePdpe = paPaePdpes[i];
2928
2929 /*
2930 * In some cases (e.g. in SVM with nested paging) the validation of the PAE PDPEs
2931 * are deferred.[1] Also, different situations require different handling of invalid
2932 * PDPE entries. Here we assume the caller has already validated or doesn't require
2933 * validation of the PDPEs.
2934 *
2935 * In the case of nested EPT (i.e. for nested-guests), the PAE PDPEs have been
2936 * validated by the VMX transition.
2937 *
2938 * [1] -- See AMD spec. 15.25.10 "Legacy PAE Mode".
2939 */
2940 if ((PaePdpe.u & (pVCpu->pgm.s.fGstPaeMbzPdpeMask | X86_PDPE_P)) == X86_PDPE_P)
2941 {
2942 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
2943 RTHCPTR HCPtr;
2944
2945 RTGCPHYS GCPhys;
2946#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
2947 if (pVCpu->pgm.s.enmGuestSlatMode == PGMSLAT_EPT)
2948 {
2949 PGMPTWALK Walk;
2950 PGMPTWALKGST GstWalk;
2951 RTGCPHYS const GCPhysNested = PaePdpe.u & X86_PDPE_PG_MASK;
2952 int const rc = pgmGstSlatWalk(pVCpu, GCPhysNested, false /* fIsLinearAddrValid */, 0 /* GCPtrNested */,
2953 &Walk, &GstWalk);
2954 if (RT_SUCCESS(rc))
2955 GCPhys = Walk.GCPhys;
2956 else
2957 {
2958 /*
2959 * Second-level address translation of the PAE PDPE has failed but we must -NOT-
2960 * abort and return a failure now. This is because we're called from a Mov CRx
2961 * instruction (or similar operation). Let's just pretend success but flag that
2962 * we need to map this PDPE lazily later.
2963 *
2964 * See Intel spec. 25.3 "Changes to instruction behavior in VMX non-root operation".
2965 * See Intel spec. 28.3.1 "EPT Overview".
2966 */
2967 pVCpu->pgm.s.apGstPaePDsR3[i] = 0;
2968 pVCpu->pgm.s.apGstPaePDsR0[i] = 0;
2969 pVCpu->pgm.s.aGCPhysGstPaePDs[i] = NIL_RTGCPHYS;
2970 continue;
2971 }
2972 }
2973 else
2974#endif
2975 {
2976 GCPhys = PGM_A20_APPLY(pVCpu, PaePdpe.u & X86_PDPE_PG_MASK);
2977 }
2978
2979 PGM_LOCK_VOID(pVM);
2980 PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhys);
2981 AssertReturnStmt(pPage, PGM_UNLOCK(pVM), VERR_PGM_INVALID_PDPE_ADDR);
2982 int const rc = pgmPhysGCPhys2CCPtrInternalDepr(pVM, pPage, GCPhys, (void **)&HCPtr);
2983 PGM_UNLOCK(pVM);
2984 if (RT_SUCCESS(rc))
2985 {
2986#ifdef IN_RING3
2987 pVCpu->pgm.s.apGstPaePDsR3[i] = (PX86PDPAE)HCPtr;
2988 pVCpu->pgm.s.apGstPaePDsR0[i] = NIL_RTR0PTR;
2989#else
2990 pVCpu->pgm.s.apGstPaePDsR3[i] = NIL_RTR3PTR;
2991 pVCpu->pgm.s.apGstPaePDsR0[i] = (PX86PDPAE)HCPtr;
2992#endif
2993 pVCpu->pgm.s.aGCPhysGstPaePDs[i] = GCPhys;
2994 continue;
2995 }
2996 AssertMsgFailed(("PGMPhysMapPaePdpes: rc2=%d GCPhys=%RGp i=%d\n", rc, GCPhys, i));
2997 }
2998 pVCpu->pgm.s.apGstPaePDsR3[i] = 0;
2999 pVCpu->pgm.s.apGstPaePDsR0[i] = 0;
3000 pVCpu->pgm.s.aGCPhysGstPaePDs[i] = NIL_RTGCPHYS;
3001 }
3002 return VINF_SUCCESS;
3003}
3004
3005
3006/**
3007 * Validates and maps the PDPT and PAE PDPEs referenced by the given CR3.
3008 *
3009 * @returns VBox status code.
3010 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
3011 * @param cr3 The guest CR3 value.
3012 *
3013 * @remarks This function may be invoked during the process of changing the guest
3014 * paging mode to PAE but the guest state (CR0, CR4 etc.) may not reflect
3015 * PAE paging just yet.
3016 */
3017VMM_INT_DECL(int) PGMGstMapPaePdpesAtCr3(PVMCPUCC pVCpu, uint64_t cr3)
3018{
3019 /*
3020 * Read the page-directory-pointer table (PDPT) at CR3.
3021 */
3022 RTGCPHYS GCPhysCR3 = (cr3 & X86_CR3_PAE_PAGE_MASK);
3023 PGM_A20_APPLY_TO_VAR(pVCpu, GCPhysCR3);
3024
3025#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
3026 if (pVCpu->pgm.s.enmGuestSlatMode == PGMSLAT_EPT)
3027 {
3028 RTGCPHYS GCPhysOut;
3029 int const rc = pgmGstSlatTranslateCr3(pVCpu, GCPhysCR3, &GCPhysOut);
3030 if (RT_SUCCESS(rc))
3031 GCPhysCR3 = GCPhysOut;
3032 else
3033 {
3034 Log(("Failed to load CR3 at %#RX64. rc=%Rrc\n", GCPhysCR3, rc));
3035 return rc;
3036 }
3037 }
3038#endif
3039
3040 RTHCPTR HCPtrGuestCr3;
3041 int rc = pgmGstMapCr3(pVCpu, GCPhysCR3, &HCPtrGuestCr3);
3042 if (RT_SUCCESS(rc))
3043 {
3044 /*
3045 * Validate the page-directory-pointer table entries (PDPE).
3046 */
3047 X86PDPE aPaePdpes[X86_PG_PAE_PDPE_ENTRIES];
3048 memcpy(&aPaePdpes[0], HCPtrGuestCr3, sizeof(aPaePdpes));
3049 if (PGMGstArePaePdpesValid(pVCpu, &aPaePdpes[0]))
3050 {
3051 /*
3052 * Map the PDPT.
3053 * We deliberately don't update PGM's GCPhysCR3 here as it's expected
3054 * that PGMFlushTLB will be called soon and only a change to CR3 then
3055 * will cause the shadow page tables to be updated.
3056 */
3057#ifdef IN_RING3
3058 pVCpu->pgm.s.pGstPaePdptR3 = (PX86PDPT)HCPtrGuestCr3;
3059 pVCpu->pgm.s.pGstPaePdptR0 = NIL_RTR0PTR;
3060#else
3061 pVCpu->pgm.s.pGstPaePdptR3 = NIL_RTR3PTR;
3062 pVCpu->pgm.s.pGstPaePdptR0 = (PX86PDPT)HCPtrGuestCr3;
3063#endif
3064
3065 /*
3066 * Update CPUM and map the 4 PAE PDPEs.
3067 */
3068 CPUMSetGuestPaePdpes(pVCpu, &aPaePdpes[0]);
3069 rc = PGMGstMapPaePdpes(pVCpu, &aPaePdpes[0]);
3070 if (RT_SUCCESS(rc))
3071 {
3072#ifdef IN_RING3
3073 pVCpu->pgm.s.fPaePdpesAndCr3MappedR3 = true;
3074 pVCpu->pgm.s.fPaePdpesAndCr3MappedR0 = false;
3075#else
3076 pVCpu->pgm.s.fPaePdpesAndCr3MappedR3 = false;
3077 pVCpu->pgm.s.fPaePdpesAndCr3MappedR0 = true;
3078#endif
3079 pVCpu->pgm.s.GCPhysPaeCR3 = GCPhysCR3;
3080 }
3081 }
3082 else
3083 rc = VERR_PGM_PAE_PDPE_RSVD;
3084 }
3085 return rc;
3086}
3087
3088
3089/**
3090 * Called whenever CR0 or CR4 in a way which may affect the paging mode.
3091 *
3092 * @returns VBox status code, with the following informational code for
3093 * VM scheduling.
3094 * @retval VINF_SUCCESS if the was no change, or it was successfully dealt with.
3095 * @retval VINF_EM_SUSPEND or VINF_EM_OFF on a fatal runtime error. (R3 only)
3096 *
3097 * @param pVCpu The cross context virtual CPU structure.
3098 * @param cr0 The new cr0.
3099 * @param cr4 The new cr4.
3100 * @param efer The new extended feature enable register.
3101 * @param fForce Whether to force a mode change.
3102 */
3103VMMDECL(int) PGMChangeMode(PVMCPUCC pVCpu, uint64_t cr0, uint64_t cr4, uint64_t efer, bool fForce)
3104{
3105 VMCPU_ASSERT_EMT(pVCpu);
3106
3107 /*
3108 * Calc the new guest mode.
3109 *
3110 * Note! We check PG before PE and without requiring PE because of the
3111 * special AMD-V paged real mode (APM vol 2, rev 3.28, 15.9).
3112 */
3113 PGMMODE enmGuestMode;
3114 if (cr0 & X86_CR0_PG)
3115 {
3116 if (!(cr4 & X86_CR4_PAE))
3117 {
3118 bool const fPse = !!(cr4 & X86_CR4_PSE);
3119 if (pVCpu->pgm.s.fGst32BitPageSizeExtension != fPse)
3120 Log(("PGMChangeMode: CR4.PSE %d -> %d\n", pVCpu->pgm.s.fGst32BitPageSizeExtension, fPse));
3121 pVCpu->pgm.s.fGst32BitPageSizeExtension = fPse;
3122 enmGuestMode = PGMMODE_32_BIT;
3123 }
3124 else if (!(efer & MSR_K6_EFER_LME))
3125 {
3126 if (!(efer & MSR_K6_EFER_NXE))
3127 enmGuestMode = PGMMODE_PAE;
3128 else
3129 enmGuestMode = PGMMODE_PAE_NX;
3130 }
3131 else
3132 {
3133 if (!(efer & MSR_K6_EFER_NXE))
3134 enmGuestMode = PGMMODE_AMD64;
3135 else
3136 enmGuestMode = PGMMODE_AMD64_NX;
3137 }
3138 }
3139 else if (!(cr0 & X86_CR0_PE))
3140 enmGuestMode = PGMMODE_REAL;
3141 else
3142 enmGuestMode = PGMMODE_PROTECTED;
3143
3144 /*
3145 * Did it change?
3146 */
3147 if ( !fForce
3148 && pVCpu->pgm.s.enmGuestMode == enmGuestMode)
3149 return VINF_SUCCESS;
3150
3151 /* Flush the TLB */
3152 PGM_INVL_VCPU_TLBS(pVCpu);
3153 return PGMHCChangeMode(pVCpu->CTX_SUFF(pVM), pVCpu, enmGuestMode, fForce);
3154}
3155
3156
3157/**
3158 * Converts a PGMMODE value to a PGM_TYPE_* \#define.
3159 *
3160 * @returns PGM_TYPE_*.
3161 * @param pgmMode The mode value to convert.
3162 */
3163DECLINLINE(unsigned) pgmModeToType(PGMMODE pgmMode)
3164{
3165 switch (pgmMode)
3166 {
3167 case PGMMODE_REAL: return PGM_TYPE_REAL;
3168 case PGMMODE_PROTECTED: return PGM_TYPE_PROT;
3169 case PGMMODE_32_BIT: return PGM_TYPE_32BIT;
3170 case PGMMODE_PAE:
3171 case PGMMODE_PAE_NX: return PGM_TYPE_PAE;
3172 case PGMMODE_AMD64:
3173 case PGMMODE_AMD64_NX: return PGM_TYPE_AMD64;
3174 case PGMMODE_NESTED_32BIT: return PGM_TYPE_NESTED_32BIT;
3175 case PGMMODE_NESTED_PAE: return PGM_TYPE_NESTED_PAE;
3176 case PGMMODE_NESTED_AMD64: return PGM_TYPE_NESTED_AMD64;
3177 case PGMMODE_EPT: return PGM_TYPE_EPT;
3178 case PGMMODE_NONE: return PGM_TYPE_NONE;
3179 default:
3180 AssertFatalMsgFailed(("pgmMode=%d\n", pgmMode));
3181 }
3182}
3183
3184
3185/**
3186 * Calculates the shadow paging mode.
3187 *
3188 * @returns The shadow paging mode.
3189 * @param pVM The cross context VM structure.
3190 * @param enmGuestMode The guest mode.
3191 * @param enmHostMode The host mode.
3192 * @param enmShadowMode The current shadow mode.
3193 */
3194static PGMMODE pgmCalcShadowMode(PVMCC pVM, PGMMODE enmGuestMode, SUPPAGINGMODE enmHostMode, PGMMODE enmShadowMode)
3195{
3196 switch (enmGuestMode)
3197 {
3198 case PGMMODE_REAL:
3199 case PGMMODE_PROTECTED:
3200 switch (enmHostMode)
3201 {
3202 case SUPPAGINGMODE_32_BIT:
3203 case SUPPAGINGMODE_32_BIT_GLOBAL:
3204 enmShadowMode = PGMMODE_32_BIT;
3205 break;
3206
3207 case SUPPAGINGMODE_PAE:
3208 case SUPPAGINGMODE_PAE_NX:
3209 case SUPPAGINGMODE_PAE_GLOBAL:
3210 case SUPPAGINGMODE_PAE_GLOBAL_NX:
3211 enmShadowMode = PGMMODE_PAE;
3212 break;
3213
3214 case SUPPAGINGMODE_AMD64:
3215 case SUPPAGINGMODE_AMD64_GLOBAL:
3216 case SUPPAGINGMODE_AMD64_NX:
3217 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
3218 enmShadowMode = PGMMODE_PAE;
3219 break;
3220
3221 default:
3222 AssertLogRelMsgFailedReturn(("enmHostMode=%d\n", enmHostMode), PGMMODE_INVALID);
3223 }
3224 break;
3225
3226 case PGMMODE_32_BIT:
3227 switch (enmHostMode)
3228 {
3229 case SUPPAGINGMODE_32_BIT:
3230 case SUPPAGINGMODE_32_BIT_GLOBAL:
3231 enmShadowMode = PGMMODE_32_BIT;
3232 break;
3233
3234 case SUPPAGINGMODE_PAE:
3235 case SUPPAGINGMODE_PAE_NX:
3236 case SUPPAGINGMODE_PAE_GLOBAL:
3237 case SUPPAGINGMODE_PAE_GLOBAL_NX:
3238 enmShadowMode = PGMMODE_PAE;
3239 break;
3240
3241 case SUPPAGINGMODE_AMD64:
3242 case SUPPAGINGMODE_AMD64_GLOBAL:
3243 case SUPPAGINGMODE_AMD64_NX:
3244 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
3245 enmShadowMode = PGMMODE_PAE;
3246 break;
3247
3248 default:
3249 AssertLogRelMsgFailedReturn(("enmHostMode=%d\n", enmHostMode), PGMMODE_INVALID);
3250 }
3251 break;
3252
3253 case PGMMODE_PAE:
3254 case PGMMODE_PAE_NX: /** @todo This might require more switchers and guest+both modes. */
3255 switch (enmHostMode)
3256 {
3257 case SUPPAGINGMODE_32_BIT:
3258 case SUPPAGINGMODE_32_BIT_GLOBAL:
3259 enmShadowMode = PGMMODE_PAE;
3260 break;
3261
3262 case SUPPAGINGMODE_PAE:
3263 case SUPPAGINGMODE_PAE_NX:
3264 case SUPPAGINGMODE_PAE_GLOBAL:
3265 case SUPPAGINGMODE_PAE_GLOBAL_NX:
3266 enmShadowMode = PGMMODE_PAE;
3267 break;
3268
3269 case SUPPAGINGMODE_AMD64:
3270 case SUPPAGINGMODE_AMD64_GLOBAL:
3271 case SUPPAGINGMODE_AMD64_NX:
3272 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
3273 enmShadowMode = PGMMODE_PAE;
3274 break;
3275
3276 default:
3277 AssertLogRelMsgFailedReturn(("enmHostMode=%d\n", enmHostMode), PGMMODE_INVALID);
3278 }
3279 break;
3280
3281 case PGMMODE_AMD64:
3282 case PGMMODE_AMD64_NX:
3283 switch (enmHostMode)
3284 {
3285 case SUPPAGINGMODE_32_BIT:
3286 case SUPPAGINGMODE_32_BIT_GLOBAL:
3287 enmShadowMode = PGMMODE_AMD64;
3288 break;
3289
3290 case SUPPAGINGMODE_PAE:
3291 case SUPPAGINGMODE_PAE_NX:
3292 case SUPPAGINGMODE_PAE_GLOBAL:
3293 case SUPPAGINGMODE_PAE_GLOBAL_NX:
3294 enmShadowMode = PGMMODE_AMD64;
3295 break;
3296
3297 case SUPPAGINGMODE_AMD64:
3298 case SUPPAGINGMODE_AMD64_GLOBAL:
3299 case SUPPAGINGMODE_AMD64_NX:
3300 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
3301 enmShadowMode = PGMMODE_AMD64;
3302 break;
3303
3304 default:
3305 AssertLogRelMsgFailedReturn(("enmHostMode=%d\n", enmHostMode), PGMMODE_INVALID);
3306 }
3307 break;
3308
3309 default:
3310 AssertLogRelMsgFailedReturn(("enmGuestMode=%d\n", enmGuestMode), PGMMODE_INVALID);
3311 }
3312
3313 /*
3314 * Override the shadow mode when NEM, IEM or nested paging is active.
3315 */
3316 if (!VM_IS_HM_ENABLED(pVM))
3317 {
3318 Assert(VM_IS_NEM_ENABLED(pVM) || VM_IS_EXEC_ENGINE_IEM(pVM));
3319 pVM->pgm.s.fNestedPaging = true;
3320 enmShadowMode = PGMMODE_NONE;
3321 }
3322 else
3323 {
3324 bool fNestedPaging = HMIsNestedPagingActive(pVM);
3325 pVM->pgm.s.fNestedPaging = fNestedPaging;
3326 if (fNestedPaging)
3327 {
3328 if (HMIsVmxActive(pVM))
3329 enmShadowMode = PGMMODE_EPT;
3330 else
3331 {
3332 /* The nested SVM paging depends on the host one. */
3333 Assert(HMIsSvmActive(pVM));
3334 if ( enmGuestMode == PGMMODE_AMD64
3335 || enmGuestMode == PGMMODE_AMD64_NX)
3336 enmShadowMode = PGMMODE_NESTED_AMD64;
3337 else
3338 switch (pVM->pgm.s.enmHostMode)
3339 {
3340 case SUPPAGINGMODE_32_BIT:
3341 case SUPPAGINGMODE_32_BIT_GLOBAL:
3342 enmShadowMode = PGMMODE_NESTED_32BIT;
3343 break;
3344
3345 case SUPPAGINGMODE_PAE:
3346 case SUPPAGINGMODE_PAE_GLOBAL:
3347 case SUPPAGINGMODE_PAE_NX:
3348 case SUPPAGINGMODE_PAE_GLOBAL_NX:
3349 enmShadowMode = PGMMODE_NESTED_PAE;
3350 break;
3351
3352 case SUPPAGINGMODE_AMD64:
3353 case SUPPAGINGMODE_AMD64_GLOBAL:
3354 case SUPPAGINGMODE_AMD64_NX:
3355 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
3356 enmShadowMode = PGMMODE_NESTED_AMD64;
3357 break;
3358
3359 default:
3360 AssertLogRelMsgFailedReturn(("enmHostMode=%d\n", pVM->pgm.s.enmHostMode), PGMMODE_INVALID);
3361 }
3362 }
3363 }
3364#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
3365 else
3366 {
3367 /* Nested paging is a requirement for nested VT-x. */
3368 AssertLogRelMsgReturn(enmGuestMode != PGMMODE_EPT, ("enmHostMode=%d\n", pVM->pgm.s.enmHostMode), PGMMODE_INVALID);
3369 }
3370#endif
3371 }
3372
3373 return enmShadowMode;
3374}
3375
3376
3377/**
3378 * Performs the actual mode change.
3379 * This is called by PGMChangeMode and pgmR3InitPaging().
3380 *
3381 * @returns VBox status code. May suspend or power off the VM on error, but this
3382 * will trigger using FFs and not informational status codes.
3383 *
3384 * @param pVM The cross context VM structure.
3385 * @param pVCpu The cross context virtual CPU structure.
3386 * @param enmGuestMode The new guest mode. This is assumed to be different from
3387 * the current mode.
3388 * @param fForce Whether to force a shadow paging mode change.
3389 */
3390VMM_INT_DECL(int) PGMHCChangeMode(PVMCC pVM, PVMCPUCC pVCpu, PGMMODE enmGuestMode, bool fForce)
3391{
3392 Log(("PGMHCChangeMode: Guest mode: %s -> %s\n", PGMGetModeName(pVCpu->pgm.s.enmGuestMode), PGMGetModeName(enmGuestMode)));
3393 STAM_REL_COUNTER_INC(&pVCpu->pgm.s.cGuestModeChanges);
3394
3395 /*
3396 * Calc the shadow mode and switcher.
3397 */
3398 PGMMODE const enmShadowMode = pgmCalcShadowMode(pVM, enmGuestMode, pVM->pgm.s.enmHostMode, pVCpu->pgm.s.enmShadowMode);
3399 bool const fShadowModeChanged = enmShadowMode != pVCpu->pgm.s.enmShadowMode || fForce;
3400
3401 /*
3402 * Exit old mode(s).
3403 */
3404 /* shadow */
3405 if (fShadowModeChanged)
3406 {
3407 LogFlow(("PGMHCChangeMode: Shadow mode: %s -> %s\n", PGMGetModeName(pVCpu->pgm.s.enmShadowMode), PGMGetModeName(enmShadowMode)));
3408 uintptr_t idxOldShw = pVCpu->pgm.s.idxShadowModeData;
3409 if ( idxOldShw < RT_ELEMENTS(g_aPgmShadowModeData)
3410 && g_aPgmShadowModeData[idxOldShw].pfnExit)
3411 {
3412 int rc = g_aPgmShadowModeData[idxOldShw].pfnExit(pVCpu);
3413 AssertMsgRCReturn(rc, ("Exit failed for shadow mode %d: %Rrc\n", pVCpu->pgm.s.enmShadowMode, rc), rc);
3414 }
3415 }
3416 else
3417 LogFlow(("PGMHCChangeMode: Shadow mode remains: %s\n", PGMGetModeName(pVCpu->pgm.s.enmShadowMode)));
3418
3419 /* guest */
3420 uintptr_t const idxOldGst = pVCpu->pgm.s.idxGuestModeData;
3421 if ( idxOldGst < RT_ELEMENTS(g_aPgmGuestModeData)
3422 && g_aPgmGuestModeData[idxOldGst].pfnExit)
3423 {
3424 int rc = g_aPgmGuestModeData[idxOldGst].pfnExit(pVCpu);
3425 AssertMsgReturn(RT_SUCCESS(rc), ("Exit failed for guest mode %d: %Rrc\n", pVCpu->pgm.s.enmGuestMode, rc), rc);
3426 }
3427 pVCpu->pgm.s.GCPhysCR3 = NIL_RTGCPHYS;
3428 pVCpu->pgm.s.GCPhysNstGstCR3 = NIL_RTGCPHYS;
3429 pVCpu->pgm.s.GCPhysPaeCR3 = NIL_RTGCPHYS;
3430 Assert(!pVCpu->pgm.s.CTX_SUFF(fPaePdpesAndCr3Mapped));
3431
3432 /*
3433 * Change the paging mode data indexes.
3434 */
3435 uintptr_t idxNewGst = pVCpu->pgm.s.idxGuestModeData = pgmModeToType(enmGuestMode);
3436 AssertReturn(idxNewGst < RT_ELEMENTS(g_aPgmGuestModeData), VERR_PGM_MODE_IPE);
3437 AssertReturn(g_aPgmGuestModeData[idxNewGst].uType == idxNewGst, VERR_PGM_MODE_IPE);
3438 AssertPtrReturn(g_aPgmGuestModeData[idxNewGst].pfnGetPage, VERR_PGM_MODE_IPE);
3439 AssertPtrReturn(g_aPgmGuestModeData[idxNewGst].pfnModifyPage, VERR_PGM_MODE_IPE);
3440 AssertPtrReturn(g_aPgmGuestModeData[idxNewGst].pfnExit, VERR_PGM_MODE_IPE);
3441 AssertPtrReturn(g_aPgmGuestModeData[idxNewGst].pfnEnter, VERR_PGM_MODE_IPE);
3442#ifdef IN_RING3
3443 AssertPtrReturn(g_aPgmGuestModeData[idxNewGst].pfnRelocate, VERR_PGM_MODE_IPE);
3444#endif
3445
3446 uintptr_t const idxNewShw = pVCpu->pgm.s.idxShadowModeData = pgmModeToType(enmShadowMode);
3447 AssertReturn(idxNewShw < RT_ELEMENTS(g_aPgmShadowModeData), VERR_PGM_MODE_IPE);
3448 AssertReturn(g_aPgmShadowModeData[idxNewShw].uType == idxNewShw, VERR_PGM_MODE_IPE);
3449 AssertPtrReturn(g_aPgmShadowModeData[idxNewShw].pfnGetPage, VERR_PGM_MODE_IPE);
3450 AssertPtrReturn(g_aPgmShadowModeData[idxNewShw].pfnModifyPage, VERR_PGM_MODE_IPE);
3451 AssertPtrReturn(g_aPgmShadowModeData[idxNewShw].pfnExit, VERR_PGM_MODE_IPE);
3452 AssertPtrReturn(g_aPgmShadowModeData[idxNewShw].pfnEnter, VERR_PGM_MODE_IPE);
3453#ifdef IN_RING3
3454 AssertPtrReturn(g_aPgmShadowModeData[idxNewShw].pfnRelocate, VERR_PGM_MODE_IPE);
3455#endif
3456
3457 uintptr_t const idxNewBth = pVCpu->pgm.s.idxBothModeData = (idxNewShw - PGM_TYPE_FIRST_SHADOW) * PGM_TYPE_END + idxNewGst;
3458 AssertReturn(g_aPgmBothModeData[idxNewBth].uShwType == idxNewShw, VERR_PGM_MODE_IPE);
3459 AssertReturn(g_aPgmBothModeData[idxNewBth].uGstType == idxNewGst, VERR_PGM_MODE_IPE);
3460 AssertPtrReturn(g_aPgmBothModeData[idxNewBth].pfnInvalidatePage, VERR_PGM_MODE_IPE);
3461 AssertPtrReturn(g_aPgmBothModeData[idxNewBth].pfnSyncCR3, VERR_PGM_MODE_IPE);
3462 AssertPtrReturn(g_aPgmBothModeData[idxNewBth].pfnPrefetchPage, VERR_PGM_MODE_IPE);
3463 AssertPtrReturn(g_aPgmBothModeData[idxNewBth].pfnVerifyAccessSyncPage, VERR_PGM_MODE_IPE);
3464 AssertPtrReturn(g_aPgmBothModeData[idxNewBth].pfnMapCR3, VERR_PGM_MODE_IPE);
3465 AssertPtrReturn(g_aPgmBothModeData[idxNewBth].pfnUnmapCR3, VERR_PGM_MODE_IPE);
3466 AssertPtrReturn(g_aPgmBothModeData[idxNewBth].pfnEnter, VERR_PGM_MODE_IPE);
3467#ifdef VBOX_STRICT
3468 AssertPtrReturn(g_aPgmBothModeData[idxNewBth].pfnAssertCR3, VERR_PGM_MODE_IPE);
3469#endif
3470
3471 /*
3472 * Determine SLAT mode -before- entering the new shadow mode!
3473 */
3474 pVCpu->pgm.s.enmGuestSlatMode = !CPUMIsGuestVmxEptPagingEnabled(pVCpu) ? PGMSLAT_DIRECT : PGMSLAT_EPT;
3475
3476 /*
3477 * Enter new shadow mode (if changed).
3478 */
3479 if (fShadowModeChanged)
3480 {
3481 pVCpu->pgm.s.enmShadowMode = enmShadowMode;
3482 int rc = g_aPgmShadowModeData[idxNewShw].pfnEnter(pVCpu);
3483 AssertLogRelMsgRCReturnStmt(rc, ("Entering enmShadowMode=%s failed: %Rrc\n", PGMGetModeName(enmShadowMode), rc),
3484 pVCpu->pgm.s.enmShadowMode = PGMMODE_INVALID, rc);
3485 }
3486
3487 /*
3488 * Always flag the necessary updates
3489 */
3490 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
3491
3492 /*
3493 * Enter the new guest and shadow+guest modes.
3494 */
3495 /* Calc the new CR3 value. */
3496 RTGCPHYS GCPhysCR3;
3497 switch (enmGuestMode)
3498 {
3499 case PGMMODE_REAL:
3500 case PGMMODE_PROTECTED:
3501 GCPhysCR3 = NIL_RTGCPHYS;
3502 break;
3503
3504 case PGMMODE_32_BIT:
3505 GCPhysCR3 = CPUMGetGuestCR3(pVCpu) & X86_CR3_PAGE_MASK;
3506 break;
3507
3508 case PGMMODE_PAE_NX:
3509 case PGMMODE_PAE:
3510 if (!pVM->cpum.ro.GuestFeatures.fPae)
3511#ifdef IN_RING3 /** @todo r=bird: wrong place, probably hasn't really worked for a while. */
3512 return VMSetRuntimeError(pVM, VMSETRTERR_FLAGS_FATAL, "PAEmode",
3513 N_("The guest is trying to switch to the PAE mode which is currently disabled by default in VirtualBox. PAE support can be enabled using the VM settings (System/Processor)"));
3514#else
3515 AssertLogRelMsgFailedReturn(("enmGuestMode=%s - Try enable PAE for the guest!\n", PGMGetModeName(enmGuestMode)), VERR_PGM_MODE_IPE);
3516
3517#endif
3518 GCPhysCR3 = CPUMGetGuestCR3(pVCpu) & X86_CR3_PAE_PAGE_MASK;
3519 break;
3520
3521#ifdef VBOX_WITH_64_BITS_GUESTS
3522 case PGMMODE_AMD64_NX:
3523 case PGMMODE_AMD64:
3524 GCPhysCR3 = CPUMGetGuestCR3(pVCpu) & X86_CR3_AMD64_PAGE_MASK;
3525 break;
3526#endif
3527 default:
3528 AssertLogRelMsgFailedReturn(("enmGuestMode=%d\n", enmGuestMode), VERR_PGM_MODE_IPE);
3529 }
3530
3531#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
3532 /*
3533 * If a nested-guest is using EPT paging:
3534 * - Update the second-level address translation (SLAT) mode.
3535 * - Indicate that the CR3 is nested-guest physical address.
3536 */
3537 if (pVCpu->pgm.s.enmGuestSlatMode == PGMSLAT_EPT)
3538 {
3539 if (PGMMODE_WITH_PAGING(enmGuestMode))
3540 {
3541 /*
3542 * Translate CR3 to its guest-physical address.
3543 * We don't use pgmGstSlatTranslateCr3() here as we want to update GCPhysNstGstCR3 -after-
3544 * switching modes to keep it consistent with how GCPhysCR3 is updated.
3545 */
3546 PGMPTWALK Walk;
3547 PGMPTWALKGST GstWalk;
3548 int const rc = pgmGstSlatWalk(pVCpu, GCPhysCR3, false /* fIsLinearAddrValid */, 0 /* GCPtrNested */, &Walk,
3549 &GstWalk);
3550 if (RT_SUCCESS(rc))
3551 { /* likely */ }
3552 else
3553 {
3554 /*
3555 * SLAT failed but we avoid reporting this to the caller because the caller
3556 * is not supposed to fail. The only time the caller needs to indicate a
3557 * failure to software is when PAE paging is used by the nested-guest, but
3558 * we handle the PAE case separately (e.g., see VMX transition in IEM).
3559 * In all other cases, the failure will be indicated when CR3 tries to be
3560 * translated on the next linear-address memory access.
3561 * See Intel spec. 27.2.1 "EPT Overview".
3562 */
3563 Log(("SLAT failed for CR3 %#RX64 rc=%Rrc\n", GCPhysCR3, rc));
3564
3565 /* Trying to coax PGM to succeed for the time being... */
3566 Assert(pVCpu->pgm.s.GCPhysCR3 == NIL_RTGCPHYS);
3567 pVCpu->pgm.s.GCPhysNstGstCR3 = GCPhysCR3;
3568 pVCpu->pgm.s.enmGuestMode = enmGuestMode;
3569 HMHCChangedPagingMode(pVM, pVCpu, pVCpu->pgm.s.enmShadowMode, pVCpu->pgm.s.enmGuestMode);
3570 return VINF_SUCCESS;
3571 }
3572 pVCpu->pgm.s.GCPhysNstGstCR3 = GCPhysCR3;
3573 GCPhysCR3 = Walk.GCPhys & X86_CR3_EPT_PAGE_MASK;
3574 }
3575 }
3576 else
3577 Assert(pVCpu->pgm.s.GCPhysNstGstCR3 == NIL_RTGCPHYS);
3578#endif
3579
3580 /*
3581 * Enter the new guest mode.
3582 */
3583 pVCpu->pgm.s.enmGuestMode = enmGuestMode;
3584 int rc = g_aPgmGuestModeData[idxNewGst].pfnEnter(pVCpu, GCPhysCR3);
3585 int rc2 = g_aPgmBothModeData[idxNewBth].pfnEnter(pVCpu, GCPhysCR3);
3586
3587 /* Set the new guest CR3 (and nested-guest CR3). */
3588 pVCpu->pgm.s.GCPhysCR3 = GCPhysCR3;
3589
3590 /* status codes. */
3591 AssertRC(rc);
3592 AssertRC(rc2);
3593 if (RT_SUCCESS(rc))
3594 {
3595 rc = rc2;
3596 if (RT_SUCCESS(rc)) /* no informational status codes. */
3597 rc = VINF_SUCCESS;
3598 }
3599
3600 /*
3601 * Notify HM.
3602 */
3603 HMHCChangedPagingMode(pVM, pVCpu, pVCpu->pgm.s.enmShadowMode, pVCpu->pgm.s.enmGuestMode);
3604 return rc;
3605}
3606
3607
3608/**
3609 * Called by CPUM or REM when CR0.WP changes to 1.
3610 *
3611 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
3612 * @thread EMT
3613 */
3614VMMDECL(void) PGMCr0WpEnabled(PVMCPUCC pVCpu)
3615{
3616 /*
3617 * Netware WP0+RO+US hack cleanup when WP0 -> WP1.
3618 *
3619 * Use the counter to judge whether there might be pool pages with active
3620 * hacks in them. If there are, we will be running the risk of messing up
3621 * the guest by allowing it to write to read-only pages. Thus, we have to
3622 * clear the page pool ASAP if there is the slightest chance.
3623 */
3624 if (pVCpu->pgm.s.cNetwareWp0Hacks > 0)
3625 {
3626 Assert(pVCpu->CTX_SUFF(pVM)->cCpus == 1);
3627
3628 Log(("PGMCr0WpEnabled: %llu WP0 hacks active - clearing page pool\n", pVCpu->pgm.s.cNetwareWp0Hacks));
3629 pVCpu->pgm.s.cNetwareWp0Hacks = 0;
3630 pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
3631 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
3632 }
3633}
3634
3635
3636/**
3637 * Gets the current guest paging mode.
3638 *
3639 * If you just need the CPU mode (real/protected/long), use CPUMGetGuestMode().
3640 *
3641 * @returns The current paging mode.
3642 * @param pVCpu The cross context virtual CPU structure.
3643 */
3644VMMDECL(PGMMODE) PGMGetGuestMode(PVMCPU pVCpu)
3645{
3646 return pVCpu->pgm.s.enmGuestMode;
3647}
3648
3649
3650/**
3651 * Gets the current shadow paging mode.
3652 *
3653 * @returns The current paging mode.
3654 * @param pVCpu The cross context virtual CPU structure.
3655 */
3656VMMDECL(PGMMODE) PGMGetShadowMode(PVMCPU pVCpu)
3657{
3658 return pVCpu->pgm.s.enmShadowMode;
3659}
3660
3661
3662/**
3663 * Gets the current host paging mode.
3664 *
3665 * @returns The current paging mode.
3666 * @param pVM The cross context VM structure.
3667 */
3668VMMDECL(PGMMODE) PGMGetHostMode(PVM pVM)
3669{
3670 switch (pVM->pgm.s.enmHostMode)
3671 {
3672 case SUPPAGINGMODE_32_BIT:
3673 case SUPPAGINGMODE_32_BIT_GLOBAL:
3674 return PGMMODE_32_BIT;
3675
3676 case SUPPAGINGMODE_PAE:
3677 case SUPPAGINGMODE_PAE_GLOBAL:
3678 return PGMMODE_PAE;
3679
3680 case SUPPAGINGMODE_PAE_NX:
3681 case SUPPAGINGMODE_PAE_GLOBAL_NX:
3682 return PGMMODE_PAE_NX;
3683
3684 case SUPPAGINGMODE_AMD64:
3685 case SUPPAGINGMODE_AMD64_GLOBAL:
3686 return PGMMODE_AMD64;
3687
3688 case SUPPAGINGMODE_AMD64_NX:
3689 case SUPPAGINGMODE_AMD64_GLOBAL_NX:
3690 return PGMMODE_AMD64_NX;
3691
3692 default: AssertMsgFailed(("enmHostMode=%d\n", pVM->pgm.s.enmHostMode)); break;
3693 }
3694
3695 return PGMMODE_INVALID;
3696}
3697
3698
3699/**
3700 * Get mode name.
3701 *
3702 * @returns read-only name string.
3703 * @param enmMode The mode which name is desired.
3704 */
3705VMMDECL(const char *) PGMGetModeName(PGMMODE enmMode)
3706{
3707 switch (enmMode)
3708 {
3709 case PGMMODE_REAL: return "Real";
3710 case PGMMODE_PROTECTED: return "Protected";
3711 case PGMMODE_32_BIT: return "32-bit";
3712 case PGMMODE_PAE: return "PAE";
3713 case PGMMODE_PAE_NX: return "PAE+NX";
3714 case PGMMODE_AMD64: return "AMD64";
3715 case PGMMODE_AMD64_NX: return "AMD64+NX";
3716 case PGMMODE_NESTED_32BIT: return "Nested-32";
3717 case PGMMODE_NESTED_PAE: return "Nested-PAE";
3718 case PGMMODE_NESTED_AMD64: return "Nested-AMD64";
3719 case PGMMODE_EPT: return "EPT";
3720 case PGMMODE_NONE: return "None";
3721 default: return "unknown mode value";
3722 }
3723}
3724
3725
3726#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
3727/**
3728 * Gets the SLAT mode name.
3729 *
3730 * @returns The read-only SLAT mode descriptive string.
3731 * @param enmSlatMode The SLAT mode value.
3732 */
3733VMM_INT_DECL(const char *) PGMGetSlatModeName(PGMSLAT enmSlatMode)
3734{
3735 switch (enmSlatMode)
3736 {
3737 case PGMSLAT_DIRECT: return "Direct";
3738 case PGMSLAT_EPT: return "EPT";
3739 case PGMSLAT_32BIT: return "32-bit";
3740 case PGMSLAT_PAE: return "PAE";
3741 case PGMSLAT_AMD64: return "AMD64";
3742 default: return "Unknown";
3743 }
3744}
3745#endif /* VBOX_WITH_NESTED_HWVIRT_VMX_EPT */
3746
3747
3748/**
3749 * Gets the physical address represented in the guest CR3 as PGM sees it.
3750 *
3751 * This is mainly for logging and debugging.
3752 *
3753 * @returns PGM's guest CR3 value.
3754 * @param pVCpu The cross context virtual CPU structure.
3755 */
3756VMM_INT_DECL(RTGCPHYS) PGMGetGuestCR3Phys(PVMCPU pVCpu)
3757{
3758 return pVCpu->pgm.s.GCPhysCR3;
3759}
3760
3761
3762
3763/**
3764 * Notification from CPUM that the EFER.NXE bit has changed.
3765 *
3766 * @param pVCpu The cross context virtual CPU structure of the CPU for
3767 * which EFER changed.
3768 * @param fNxe The new NXE state.
3769 */
3770VMM_INT_DECL(void) PGMNotifyNxeChanged(PVMCPU pVCpu, bool fNxe)
3771{
3772/** @todo VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu); */
3773 Log(("PGMNotifyNxeChanged: fNxe=%RTbool\n", fNxe));
3774
3775 pVCpu->pgm.s.fNoExecuteEnabled = fNxe;
3776 if (fNxe)
3777 {
3778 /*pVCpu->pgm.s.fGst32BitMbzBigPdeMask - N/A */
3779 pVCpu->pgm.s.fGstPaeMbzPteMask &= ~X86_PTE_PAE_NX;
3780 pVCpu->pgm.s.fGstPaeMbzPdeMask &= ~X86_PDE_PAE_NX;
3781 pVCpu->pgm.s.fGstPaeMbzBigPdeMask &= ~X86_PDE2M_PAE_NX;
3782 /*pVCpu->pgm.s.fGstPaeMbzPdpeMask - N/A */
3783 pVCpu->pgm.s.fGstAmd64MbzPteMask &= ~X86_PTE_PAE_NX;
3784 pVCpu->pgm.s.fGstAmd64MbzPdeMask &= ~X86_PDE_PAE_NX;
3785 pVCpu->pgm.s.fGstAmd64MbzBigPdeMask &= ~X86_PDE2M_PAE_NX;
3786 pVCpu->pgm.s.fGstAmd64MbzPdpeMask &= ~X86_PDPE_LM_NX;
3787 pVCpu->pgm.s.fGstAmd64MbzBigPdpeMask &= ~X86_PDPE_LM_NX;
3788 pVCpu->pgm.s.fGstAmd64MbzPml4eMask &= ~X86_PML4E_NX;
3789
3790 pVCpu->pgm.s.fGst64ShadowedPteMask |= X86_PTE_PAE_NX;
3791 pVCpu->pgm.s.fGst64ShadowedPdeMask |= X86_PDE_PAE_NX;
3792 pVCpu->pgm.s.fGst64ShadowedBigPdeMask |= X86_PDE2M_PAE_NX;
3793 pVCpu->pgm.s.fGst64ShadowedBigPde4PteMask |= X86_PDE2M_PAE_NX;
3794 pVCpu->pgm.s.fGstAmd64ShadowedPdpeMask |= X86_PDPE_LM_NX;
3795 pVCpu->pgm.s.fGstAmd64ShadowedPml4eMask |= X86_PML4E_NX;
3796 }
3797 else
3798 {
3799 /*pVCpu->pgm.s.fGst32BitMbzBigPdeMask - N/A */
3800 pVCpu->pgm.s.fGstPaeMbzPteMask |= X86_PTE_PAE_NX;
3801 pVCpu->pgm.s.fGstPaeMbzPdeMask |= X86_PDE_PAE_NX;
3802 pVCpu->pgm.s.fGstPaeMbzBigPdeMask |= X86_PDE2M_PAE_NX;
3803 /*pVCpu->pgm.s.fGstPaeMbzPdpeMask -N/A */
3804 pVCpu->pgm.s.fGstAmd64MbzPteMask |= X86_PTE_PAE_NX;
3805 pVCpu->pgm.s.fGstAmd64MbzPdeMask |= X86_PDE_PAE_NX;
3806 pVCpu->pgm.s.fGstAmd64MbzBigPdeMask |= X86_PDE2M_PAE_NX;
3807 pVCpu->pgm.s.fGstAmd64MbzPdpeMask |= X86_PDPE_LM_NX;
3808 pVCpu->pgm.s.fGstAmd64MbzBigPdpeMask |= X86_PDPE_LM_NX;
3809 pVCpu->pgm.s.fGstAmd64MbzPml4eMask |= X86_PML4E_NX;
3810
3811 pVCpu->pgm.s.fGst64ShadowedPteMask &= ~X86_PTE_PAE_NX;
3812 pVCpu->pgm.s.fGst64ShadowedPdeMask &= ~X86_PDE_PAE_NX;
3813 pVCpu->pgm.s.fGst64ShadowedBigPdeMask &= ~X86_PDE2M_PAE_NX;
3814 pVCpu->pgm.s.fGst64ShadowedBigPde4PteMask &= ~X86_PDE2M_PAE_NX;
3815 pVCpu->pgm.s.fGstAmd64ShadowedPdpeMask &= ~X86_PDPE_LM_NX;
3816 pVCpu->pgm.s.fGstAmd64ShadowedPml4eMask &= ~X86_PML4E_NX;
3817 }
3818}
3819
3820
3821/**
3822 * Check if any pgm pool pages are marked dirty (not monitored)
3823 *
3824 * @returns bool locked/not locked
3825 * @param pVM The cross context VM structure.
3826 */
3827VMMDECL(bool) PGMHasDirtyPages(PVM pVM)
3828{
3829 return pVM->pgm.s.CTX_SUFF(pPool)->cDirtyPages != 0;
3830}
3831
3832
3833/**
3834 * Check if this VCPU currently owns the PGM lock.
3835 *
3836 * @returns bool owner/not owner
3837 * @param pVM The cross context VM structure.
3838 */
3839VMMDECL(bool) PGMIsLockOwner(PVMCC pVM)
3840{
3841 return PDMCritSectIsOwner(pVM, &pVM->pgm.s.CritSectX);
3842}
3843
3844
3845/**
3846 * Enable or disable large page usage
3847 *
3848 * @returns VBox status code.
3849 * @param pVM The cross context VM structure.
3850 * @param fUseLargePages Use/not use large pages
3851 */
3852VMMDECL(int) PGMSetLargePageUsage(PVMCC pVM, bool fUseLargePages)
3853{
3854 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
3855
3856 pVM->pgm.s.fUseLargePages = fUseLargePages;
3857 return VINF_SUCCESS;
3858}
3859
3860
3861/**
3862 * Acquire the PGM lock.
3863 *
3864 * @returns VBox status code
3865 * @param pVM The cross context VM structure.
3866 * @param fVoid Set if the caller cannot handle failure returns.
3867 * @param SRC_POS The source position of the caller (RT_SRC_POS).
3868 */
3869#if defined(VBOX_STRICT) || defined(DOXYGEN_RUNNING)
3870int pgmLockDebug(PVMCC pVM, bool fVoid, RT_SRC_POS_DECL)
3871#else
3872int pgmLock(PVMCC pVM, bool fVoid)
3873#endif
3874{
3875#if defined(VBOX_STRICT)
3876 int rc = PDMCritSectEnterDebug(pVM, &pVM->pgm.s.CritSectX, VINF_SUCCESS, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
3877#else
3878 int rc = PDMCritSectEnter(pVM, &pVM->pgm.s.CritSectX, VINF_SUCCESS);
3879#endif
3880 if (RT_SUCCESS(rc))
3881 return rc;
3882 if (fVoid)
3883 PDM_CRITSECT_RELEASE_ASSERT_RC(pVM, &pVM->pgm.s.CritSectX, rc);
3884 else
3885 AssertRC(rc);
3886 return rc;
3887}
3888
3889
3890/**
3891 * Release the PGM lock.
3892 *
3893 * @param pVM The cross context VM structure.
3894 */
3895void pgmUnlock(PVMCC pVM)
3896{
3897 uint32_t cDeprecatedPageLocks = pVM->pgm.s.cDeprecatedPageLocks;
3898 pVM->pgm.s.cDeprecatedPageLocks = 0;
3899 int rc = PDMCritSectLeave(pVM, &pVM->pgm.s.CritSectX);
3900 if (rc == VINF_SEM_NESTED)
3901 pVM->pgm.s.cDeprecatedPageLocks = cDeprecatedPageLocks;
3902}
3903
3904
3905#if !defined(IN_R0) || defined(LOG_ENABLED)
3906
3907/** Format handler for PGMPAGE.
3908 * @copydoc FNRTSTRFORMATTYPE */
3909static DECLCALLBACK(size_t) pgmFormatTypeHandlerPage(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
3910 const char *pszType, void const *pvValue,
3911 int cchWidth, int cchPrecision, unsigned fFlags,
3912 void *pvUser)
3913{
3914 size_t cch;
3915 PCPGMPAGE pPage = (PCPGMPAGE)pvValue;
3916 if (RT_VALID_PTR(pPage))
3917 {
3918 char szTmp[64+80];
3919
3920 cch = 0;
3921
3922 /* The single char state stuff. */
3923 static const char s_achPageStates[4] = { 'Z', 'A', 'W', 'S' };
3924 szTmp[cch++] = s_achPageStates[PGM_PAGE_GET_STATE_NA(pPage)];
3925
3926# define IS_PART_INCLUDED(lvl) ( !(fFlags & RTSTR_F_PRECISION) || cchPrecision == (lvl) || cchPrecision >= (lvl)+10 )
3927 if (IS_PART_INCLUDED(5))
3928 {
3929 static const char s_achHandlerStates[4*2] = { '-', 't', 'w', 'a' , '_', 'T', 'W', 'A' };
3930 szTmp[cch++] = s_achHandlerStates[ PGM_PAGE_GET_HNDL_PHYS_STATE(pPage)
3931 | ((uint8_t)PGM_PAGE_IS_HNDL_PHYS_NOT_IN_HM(pPage) << 2)];
3932 }
3933
3934 /* The type. */
3935 if (IS_PART_INCLUDED(4))
3936 {
3937 szTmp[cch++] = ':';
3938 static const char s_achPageTypes[8][4] = { "INV", "RAM", "MI2", "M2A", "SHA", "ROM", "MIO", "BAD" };
3939 szTmp[cch++] = s_achPageTypes[PGM_PAGE_GET_TYPE_NA(pPage)][0];
3940 szTmp[cch++] = s_achPageTypes[PGM_PAGE_GET_TYPE_NA(pPage)][1];
3941 szTmp[cch++] = s_achPageTypes[PGM_PAGE_GET_TYPE_NA(pPage)][2];
3942 }
3943
3944 /* The numbers. */
3945 if (IS_PART_INCLUDED(3))
3946 {
3947 szTmp[cch++] = ':';
3948 cch += RTStrFormatNumber(&szTmp[cch], PGM_PAGE_GET_HCPHYS_NA(pPage), 16, 12, 0, RTSTR_F_ZEROPAD | RTSTR_F_64BIT);
3949 }
3950
3951 if (IS_PART_INCLUDED(2))
3952 {
3953 szTmp[cch++] = ':';
3954 cch += RTStrFormatNumber(&szTmp[cch], PGM_PAGE_GET_PAGEID(pPage), 16, 7, 0, RTSTR_F_ZEROPAD | RTSTR_F_32BIT);
3955 }
3956
3957 if (IS_PART_INCLUDED(6))
3958 {
3959 szTmp[cch++] = ':';
3960 static const char s_achRefs[4] = { '-', 'U', '!', 'L' };
3961 szTmp[cch++] = s_achRefs[PGM_PAGE_GET_TD_CREFS_NA(pPage)];
3962 cch += RTStrFormatNumber(&szTmp[cch], PGM_PAGE_GET_TD_IDX_NA(pPage), 16, 4, 0, RTSTR_F_ZEROPAD | RTSTR_F_16BIT);
3963 }
3964# undef IS_PART_INCLUDED
3965
3966 cch = pfnOutput(pvArgOutput, szTmp, cch);
3967#if 0
3968 size_t cch2 = 0;
3969 szTmp[cch2++] = '(';
3970 cch2 += RTStrFormatNumber(&szTmp[cch2], (uintptr_t)pPage, 16, 18, 0, RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD | RTSTR_F_64BIT);
3971 szTmp[cch2++] = ')';
3972 szTmp[cch2] = '\0';
3973 cch += pfnOutput(pvArgOutput, szTmp, cch2);
3974#endif
3975 }
3976 else
3977 cch = pfnOutput(pvArgOutput, RT_STR_TUPLE("<bad-pgmpage-ptr>"));
3978 NOREF(pszType); NOREF(cchWidth); NOREF(pvUser);
3979 return cch;
3980}
3981
3982
3983/** Format handler for PGMRAMRANGE.
3984 * @copydoc FNRTSTRFORMATTYPE */
3985static DECLCALLBACK(size_t) pgmFormatTypeHandlerRamRange(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
3986 const char *pszType, void const *pvValue,
3987 int cchWidth, int cchPrecision, unsigned fFlags,
3988 void *pvUser)
3989{
3990 size_t cch;
3991 PGMRAMRANGE const *pRam = (PGMRAMRANGE const *)pvValue;
3992 if (RT_VALID_PTR(pRam))
3993 {
3994 char szTmp[80];
3995 cch = RTStrPrintf(szTmp, sizeof(szTmp), "%RGp-%RGp", pRam->GCPhys, pRam->GCPhysLast);
3996 cch = pfnOutput(pvArgOutput, szTmp, cch);
3997 }
3998 else
3999 cch = pfnOutput(pvArgOutput, RT_STR_TUPLE("<bad-pgmramrange-ptr>"));
4000 NOREF(pszType); NOREF(cchWidth); NOREF(cchPrecision); NOREF(pvUser); NOREF(fFlags);
4001 return cch;
4002}
4003
4004/** Format type andlers to be registered/deregistered. */
4005static const struct
4006{
4007 char szType[24];
4008 PFNRTSTRFORMATTYPE pfnHandler;
4009} g_aPgmFormatTypes[] =
4010{
4011 { "pgmpage", pgmFormatTypeHandlerPage },
4012 { "pgmramrange", pgmFormatTypeHandlerRamRange }
4013};
4014
4015#endif /* !IN_R0 || LOG_ENABLED */
4016
4017/**
4018 * Registers the global string format types.
4019 *
4020 * This should be called at module load time or in some other manner that ensure
4021 * that it's called exactly one time.
4022 *
4023 * @returns IPRT status code on RTStrFormatTypeRegister failure.
4024 */
4025VMMDECL(int) PGMRegisterStringFormatTypes(void)
4026{
4027#if !defined(IN_R0) || defined(LOG_ENABLED)
4028 int rc = VINF_SUCCESS;
4029 unsigned i;
4030 for (i = 0; RT_SUCCESS(rc) && i < RT_ELEMENTS(g_aPgmFormatTypes); i++)
4031 {
4032 rc = RTStrFormatTypeRegister(g_aPgmFormatTypes[i].szType, g_aPgmFormatTypes[i].pfnHandler, NULL);
4033# ifdef IN_RING0
4034 if (rc == VERR_ALREADY_EXISTS)
4035 {
4036 /* in case of cleanup failure in ring-0 */
4037 RTStrFormatTypeDeregister(g_aPgmFormatTypes[i].szType);
4038 rc = RTStrFormatTypeRegister(g_aPgmFormatTypes[i].szType, g_aPgmFormatTypes[i].pfnHandler, NULL);
4039 }
4040# endif
4041 }
4042 if (RT_FAILURE(rc))
4043 while (i-- > 0)
4044 RTStrFormatTypeDeregister(g_aPgmFormatTypes[i].szType);
4045
4046 return rc;
4047#else
4048 return VINF_SUCCESS;
4049#endif
4050}
4051
4052
4053/**
4054 * Deregisters the global string format types.
4055 *
4056 * This should be called at module unload time or in some other manner that
4057 * ensure that it's called exactly one time.
4058 */
4059VMMDECL(void) PGMDeregisterStringFormatTypes(void)
4060{
4061#if !defined(IN_R0) || defined(LOG_ENABLED)
4062 for (unsigned i = 0; i < RT_ELEMENTS(g_aPgmFormatTypes); i++)
4063 RTStrFormatTypeDeregister(g_aPgmFormatTypes[i].szType);
4064#endif
4065}
4066
4067
4068#ifdef VBOX_STRICT
4069/**
4070 * Asserts that everything related to the guest CR3 is correctly shadowed.
4071 *
4072 * This will call PGMAssertNoMappingConflicts() and PGMAssertHandlerAndFlagsInSync(),
4073 * and assert the correctness of the guest CR3 mapping before asserting that the
4074 * shadow page tables is in sync with the guest page tables.
4075 *
4076 * @returns Number of conflicts.
4077 * @param pVM The cross context VM structure.
4078 * @param pVCpu The cross context virtual CPU structure.
4079 * @param cr3 The current guest CR3 register value.
4080 * @param cr4 The current guest CR4 register value.
4081 */
4082VMMDECL(unsigned) PGMAssertCR3(PVMCC pVM, PVMCPUCC pVCpu, uint64_t cr3, uint64_t cr4)
4083{
4084 STAM_PROFILE_START(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncCR3), a);
4085
4086 uintptr_t const idxBth = pVCpu->pgm.s.idxBothModeData;
4087 AssertReturn(idxBth < RT_ELEMENTS(g_aPgmBothModeData), -VERR_PGM_MODE_IPE);
4088 AssertReturn(g_aPgmBothModeData[idxBth].pfnAssertCR3, -VERR_PGM_MODE_IPE);
4089
4090 PGM_LOCK_VOID(pVM);
4091 unsigned cErrors = g_aPgmBothModeData[idxBth].pfnAssertCR3(pVCpu, cr3, cr4, 0, ~(RTGCPTR)0);
4092 PGM_UNLOCK(pVM);
4093
4094 STAM_PROFILE_STOP(&pVCpu->pgm.s.Stats.CTX_MID_Z(Stat,SyncCR3), a);
4095 return cErrors;
4096}
4097#endif /* VBOX_STRICT */
4098
4099
4100/**
4101 * Updates PGM's copy of the guest's EPT pointer.
4102 *
4103 * @param pVCpu The cross context virtual CPU structure.
4104 * @param uEptPtr The EPT pointer.
4105 *
4106 * @remarks This can be called as part of VM-entry so we might be in the midst of
4107 * switching to VMX non-root mode.
4108 */
4109VMM_INT_DECL(void) PGMSetGuestEptPtr(PVMCPUCC pVCpu, uint64_t uEptPtr)
4110{
4111 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
4112 PGM_LOCK_VOID(pVM);
4113 pVCpu->pgm.s.uEptPtr = uEptPtr;
4114 pVCpu->pgm.s.pGstEptPml4R3 = 0;
4115 pVCpu->pgm.s.pGstEptPml4R0 = 0;
4116 PGM_UNLOCK(pVM);
4117}
4118
4119#ifdef PGM_WITH_PAGE_ZEROING_DETECTION
4120
4121/**
4122 * Helper for checking whether XMM0 is zero, possibly retriving external state.
4123 */
4124static bool pgmHandlePageZeroingIsXmm0Zero(PVMCPUCC pVCpu, PCPUMCTX pCtx)
4125{
4126 if (pCtx->fExtrn & CPUMCTX_EXTRN_SSE_AVX)
4127 {
4128 int rc = CPUMImportGuestStateOnDemand(pVCpu, CPUMCTX_EXTRN_SSE_AVX);
4129 AssertRCReturn(rc, false);
4130 }
4131 return pCtx->XState.x87.aXMM[0].au64[0] == 0
4132 && pCtx->XState.x87.aXMM[0].au64[1] == 0
4133 && pCtx->XState.x87.aXMM[0].au64[2] == 0
4134 && pCtx->XState.x87.aXMM[0].au64[3] == 0;
4135}
4136
4137
4138/**
4139 * Helper for comparing opcode bytes.
4140 */
4141static bool pgmHandlePageZeroingMatchOpcodes(PVMCPUCC pVCpu, PCPUMCTX pCtx, uint8_t const *pbOpcodes, uint32_t cbOpcodes)
4142{
4143 uint8_t abTmp[64];
4144 AssertMsgReturn(cbOpcodes <= sizeof(abTmp), ("cbOpcodes=%#x\n", cbOpcodes), false);
4145 int rc = PGMPhysSimpleReadGCPtr(pVCpu, abTmp, pCtx->rip + pCtx->cs.u64Base, cbOpcodes);
4146 if (RT_SUCCESS(rc))
4147 return memcmp(abTmp, pbOpcodes, cbOpcodes) == 0;
4148 return false;
4149}
4150
4151
4152/**
4153 * Called on faults on ZERO pages to check if the guest is trying to zero it.
4154 *
4155 * Since it's a waste of time to zero a ZERO page and it will cause an
4156 * unnecessary page allocation, we'd like to detect and avoid this.
4157 * If any known page zeroing code is detected, this function will update the CPU
4158 * state to pretend the page was zeroed by the code.
4159 *
4160 * @returns true if page zeroing code was detected and CPU state updated to skip
4161 * the code.
4162 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
4163 * @param pCtx The guest register context.
4164 */
4165static bool pgmHandlePageZeroingCode(PVMCPUCC pVCpu, PCPUMCTX pCtx)
4166{
4167 CPUMCTX_ASSERT_NOT_EXTRN(pCtx, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4 | CPUMCTX_EXTRN_EFER);
4168
4169 /*
4170 * Sort by mode first.
4171 */
4172 if (CPUMIsGuestInLongModeEx(pCtx))
4173 {
4174 if (CPUMIsGuestIn64BitCodeEx(pCtx))
4175 {
4176 /*
4177 * 64-bit code.
4178 */
4179 Log9(("pgmHandlePageZeroingCode: not page zeroing - 64-bit\n"));
4180 }
4181 else if (pCtx->cs.Attr.n.u1DefBig)
4182 Log9(("pgmHandlePageZeroingCode: not page zeroing - 32-bit lm\n"));
4183 else
4184 Log9(("pgmHandlePageZeroingCode: not page zeroing - 16-bit lm\n"));
4185 }
4186 else if (CPUMIsGuestInPagedProtectedModeEx(pCtx))
4187 {
4188 if (pCtx->cs.Attr.n.u1DefBig)
4189 {
4190 /*
4191 * 32-bit paged protected mode code.
4192 */
4193 CPUMCTX_ASSERT_NOT_EXTRN(pCtx, CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RCX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RBX
4194 | CPUMCTX_EXTRN_RSP | CPUMCTX_EXTRN_RBP | CPUMCTX_EXTRN_RSI | CPUMCTX_EXTRN_RDI
4195 | CPUMCTX_EXTRN_RIP | CPUMCTX_EXTRN_RFLAGS);
4196
4197 /* 1. Generic 'rep stosd' detection. */
4198 static uint8_t const s_abRepStosD[] = { 0xf3, 0xab };
4199 if ( pCtx->eax == 0
4200 && pCtx->ecx == X86_PAGE_SIZE / 4
4201 && !(pCtx->edi & X86_PAGE_OFFSET_MASK)
4202 && pgmHandlePageZeroingMatchOpcodes(pVCpu, pCtx, s_abRepStosD, sizeof(s_abRepStosD)))
4203 {
4204 pCtx->ecx = 0;
4205 pCtx->edi += X86_PAGE_SIZE;
4206 Log9(("pgmHandlePageZeroingCode: REP STOSD: eip=%RX32 -> %RX32\n", pCtx->eip, pCtx->eip + sizeof(s_abRepStosD)));
4207 pCtx->eip += sizeof(s_abRepStosD);
4208 return true;
4209 }
4210
4211 /* 2. Windows 2000 sp4 KiXMMIZeroPageNoSave loop code: */
4212 static uint8_t const s_abW2kSp4XmmZero[] =
4213 {
4214 0x0f, 0x2b, 0x01,
4215 0x0f, 0x2b, 0x41, 0x10,
4216 0x0f, 0x2b, 0x41, 0x20,
4217 0x0f, 0x2b, 0x41, 0x30,
4218 0x83, 0xc1, 0x40,
4219 0x48,
4220 0x75, 0xeb,
4221 };
4222 if ( pCtx->eax == 64
4223 && !(pCtx->ecx & X86_PAGE_OFFSET_MASK)
4224 && pgmHandlePageZeroingMatchOpcodes(pVCpu, pCtx, s_abW2kSp4XmmZero, sizeof(s_abW2kSp4XmmZero))
4225 && pgmHandlePageZeroingIsXmm0Zero(pVCpu, pCtx))
4226 {
4227 pCtx->eax = 1;
4228 pCtx->ecx += X86_PAGE_SIZE;
4229 Log9(("pgmHandlePageZeroingCode: w2k sp4 xmm: eip=%RX32 -> %RX32\n",
4230 pCtx->eip, pCtx->eip + sizeof(s_abW2kSp4XmmZero) - 3));
4231 pCtx->eip += sizeof(s_abW2kSp4XmmZero) - 3;
4232 return true;
4233 }
4234 Log9(("pgmHandlePageZeroingCode: not page zeroing - 32-bit\n"));
4235 }
4236 else if (!pCtx->eflags.Bits.u1VM)
4237 Log9(("pgmHandlePageZeroingCode: not page zeroing - 16-bit\n"));
4238 else
4239 Log9(("pgmHandlePageZeroingCode: not page zeroing - v86\n"));
4240 }
4241 return false;
4242}
4243
4244#endif /* PGM_WITH_PAGE_ZEROING_DETECTION */
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