VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/PGMR0.cpp@ 30915

Last change on this file since 30915 was 30889, checked in by vboxsync, 15 years ago

PGM: Cleanups related to pending MMIO/#PF optimizations. Risky.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.5 KB
Line 
1/* $Id: PGMR0.cpp 30889 2010-07-17 01:54:47Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor, Ring-0.
4 */
5
6/*
7 * Copyright (C) 2007-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_PGM
22#include <VBox/pgm.h>
23#include <VBox/gmm.h>
24#include "../PGMInternal.h"
25#include <VBox/vm.h>
26#include "../PGMInline.h"
27#include <VBox/log.h>
28#include <VBox/err.h>
29#include <iprt/assert.h>
30#include <iprt/mem.h>
31
32
33/*
34 * Instantiate the ring-0 header/code templates.
35 */
36#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
37#include "PGMR0Bth.h"
38#undef PGM_BTH_NAME
39
40#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
41#include "PGMR0Bth.h"
42#undef PGM_BTH_NAME
43
44#define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
45#include "PGMR0Bth.h"
46#undef PGM_BTH_NAME
47
48#define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
49#include "PGMR0Bth.h"
50#undef PGM_BTH_NAME
51
52
53/**
54 * Worker function for PGMR3PhysAllocateHandyPages and pgmPhysEnsureHandyPage.
55 *
56 * @returns The following VBox status codes.
57 * @retval VINF_SUCCESS on success. FF cleared.
58 * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is set in this case.
59 *
60 * @param pVM The VM handle.
61 * @param pVCpu The VMCPU handle.
62 *
63 * @remarks Must be called from within the PGM critical section. The caller
64 * must clear the new pages.
65 */
66VMMR0DECL(int) PGMR0PhysAllocateHandyPages(PVM pVM, PVMCPU pVCpu)
67{
68 Assert(PDMCritSectIsOwnerEx(&pVM->pgm.s.CritSect, pVCpu->idCpu));
69
70 /*
71 * Check for error injection.
72 */
73 if (RT_UNLIKELY(pVM->pgm.s.fErrInjHandyPages))
74 return VERR_NO_MEMORY;
75
76 /*
77 * Try allocate a full set of handy pages.
78 */
79 uint32_t iFirst = pVM->pgm.s.cHandyPages;
80 AssertReturn(iFirst <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), VERR_INTERNAL_ERROR);
81 uint32_t cPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages) - iFirst;
82 if (!cPages)
83 return VINF_SUCCESS;
84 int rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
85 if (RT_SUCCESS(rc))
86 {
87 for (uint32_t i = 0; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
88 {
89 Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
90 Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
91 Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
92 Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
93 Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
94 }
95
96 pVM->pgm.s.cHandyPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages);
97 }
98 else if (rc != VERR_GMM_SEED_ME)
99 {
100 if ( ( rc == VERR_GMM_HIT_GLOBAL_LIMIT
101 || rc == VERR_GMM_HIT_VM_ACCOUNT_LIMIT)
102 && iFirst < PGM_HANDY_PAGES_MIN)
103 {
104
105#ifdef VBOX_STRICT
106 /* We're ASSUMING that GMM has updated all the entires before failing us. */
107 uint32_t i;
108 for (i = iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
109 {
110 Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
111 Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
112 Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
113 }
114#endif
115
116 /*
117 * Reduce the number of pages until we hit the minimum limit.
118 */
119 do
120 {
121 cPages >>= 2;
122 if (cPages + iFirst < PGM_HANDY_PAGES_MIN)
123 cPages = PGM_HANDY_PAGES_MIN - iFirst;
124 rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
125 } while ( ( rc == VERR_GMM_HIT_GLOBAL_LIMIT
126 || rc == VERR_GMM_HIT_VM_ACCOUNT_LIMIT)
127 && cPages + iFirst > PGM_HANDY_PAGES_MIN);
128 if (RT_SUCCESS(rc))
129 {
130#ifdef VBOX_STRICT
131 i = iFirst + cPages;
132 while (i-- > 0)
133 {
134 Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
135 Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
136 Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
137 Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
138 Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
139 }
140
141 for (i = cPages + iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
142 {
143 Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
144 Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
145 Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
146 }
147#endif
148
149 pVM->pgm.s.cHandyPages = iFirst + cPages;
150 }
151 }
152
153 if (RT_FAILURE(rc) && rc != VERR_GMM_SEED_ME)
154 {
155 LogRel(("PGMR0PhysAllocateHandyPages: rc=%Rrc iFirst=%d cPages=%d\n", rc, iFirst, cPages));
156 VM_FF_SET(pVM, VM_FF_PGM_NO_MEMORY);
157 }
158 }
159
160
161 LogFlow(("PGMR0PhysAllocateHandyPages: cPages=%d rc=%Rrc\n", cPages, rc));
162 return rc;
163}
164
165
166/**
167 * Worker function for PGMR3PhysAllocateLargeHandyPage
168 *
169 * @returns The following VBox status codes.
170 * @retval VINF_SUCCESS on success.
171 * @retval VINF_EM_NO_MEMORY if we're out of memory.
172 *
173 * @param pVM The VM handle.
174 * @param pVCpu The VMCPU handle.
175 *
176 * @remarks Must be called from within the PGM critical section. The caller
177 * must clear the new pages.
178 */
179VMMR0DECL(int) PGMR0PhysAllocateLargeHandyPage(PVM pVM, PVMCPU pVCpu)
180{
181 Assert(PDMCritSectIsOwnerEx(&pVM->pgm.s.CritSect, pVCpu->idCpu));
182
183 Assert(!pVM->pgm.s.cLargeHandyPages);
184 int rc = GMMR0AllocateLargePage(pVM, pVCpu->idCpu, _2M, &pVM->pgm.s.aLargeHandyPage[0].idPage, &pVM->pgm.s.aLargeHandyPage[0].HCPhysGCPhys);
185 if (RT_SUCCESS(rc))
186 pVM->pgm.s.cLargeHandyPages = 1;
187
188 return rc;
189}
190
191
192/**
193 * #PF Handler for nested paging.
194 *
195 * @returns VBox status code (appropriate for trap handling and GC return).
196 * @param pVM VM Handle.
197 * @param pVCpu VMCPU Handle.
198 * @param enmShwPagingMode Paging mode for the nested page tables
199 * @param uErr The trap error code.
200 * @param pRegFrame Trap register frame.
201 * @param pvFault The fault address.
202 */
203VMMR0DECL(int) PGMR0Trap0eHandlerNestedPaging(PVM pVM, PVMCPU pVCpu, PGMMODE enmShwPagingMode, RTGCUINT uErr,
204 PCPUMCTXCORE pRegFrame, RTGCPHYS pvFault)
205{
206 int rc;
207
208 LogFlow(("PGMTrap0eHandler: uErr=%RGx pvFault=%RGp eip=%RGv\n", uErr, pvFault, (RTGCPTR)pRegFrame->rip));
209 STAM_PROFILE_START(&pVCpu->pgm.s.StatRZTrap0e, a);
210 STAM_STATS({ pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = NULL; } );
211
212 /* AMD uses the host's paging mode; Intel has a single mode (EPT). */
213 AssertMsg( enmShwPagingMode == PGMMODE_32_BIT || enmShwPagingMode == PGMMODE_PAE || enmShwPagingMode == PGMMODE_PAE_NX
214 || enmShwPagingMode == PGMMODE_AMD64 || enmShwPagingMode == PGMMODE_AMD64_NX || enmShwPagingMode == PGMMODE_EPT,
215 ("enmShwPagingMode=%d\n", enmShwPagingMode));
216
217#ifdef VBOX_WITH_STATISTICS
218 /*
219 * Error code stats.
220 */
221 if (uErr & X86_TRAP_PF_US)
222 {
223 if (!(uErr & X86_TRAP_PF_P))
224 {
225 if (uErr & X86_TRAP_PF_RW)
226 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentWrite);
227 else
228 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentRead);
229 }
230 else if (uErr & X86_TRAP_PF_RW)
231 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSWrite);
232 else if (uErr & X86_TRAP_PF_RSVD)
233 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSReserved);
234 else if (uErr & X86_TRAP_PF_ID)
235 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNXE);
236 else
237 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSRead);
238 }
239 else
240 { /* Supervisor */
241 if (!(uErr & X86_TRAP_PF_P))
242 {
243 if (uErr & X86_TRAP_PF_RW)
244 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentWrite);
245 else
246 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentRead);
247 }
248 else if (uErr & X86_TRAP_PF_RW)
249 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVWrite);
250 else if (uErr & X86_TRAP_PF_ID)
251 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSNXE);
252 else if (uErr & X86_TRAP_PF_RSVD)
253 STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVReserved);
254 }
255#endif
256
257 /*
258 * Call the worker.
259 *
260 * Note! We pretend the guest is in protected mode without paging, so we
261 * can use existing code to build the nested page tables.
262 */
263 bool fLockTaken = false;
264 switch(enmShwPagingMode)
265 {
266 case PGMMODE_32_BIT:
267 rc = PGM_BTH_NAME_32BIT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
268 break;
269 case PGMMODE_PAE:
270 case PGMMODE_PAE_NX:
271 rc = PGM_BTH_NAME_PAE_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
272 break;
273 case PGMMODE_AMD64:
274 case PGMMODE_AMD64_NX:
275 rc = PGM_BTH_NAME_AMD64_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
276 break;
277 case PGMMODE_EPT:
278 rc = PGM_BTH_NAME_EPT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
279 break;
280 default:
281 AssertFailed();
282 rc = VERR_INVALID_PARAMETER;
283 break;
284 }
285 if (fLockTaken)
286 {
287 Assert(PGMIsLockOwner(pVM));
288 pgmUnlock(pVM);
289 }
290
291 if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
292 rc = VINF_SUCCESS;
293 /* Note: hack alert for difficult to reproduce problem. */
294 else if ( rc == VERR_PAGE_NOT_PRESENT /* SMP only ; disassembly might fail. */
295 || rc == VERR_PAGE_TABLE_NOT_PRESENT /* seen with UNI & SMP */
296 || rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT /* seen with SMP */
297 || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT) /* precaution */
298 {
299 Log(("WARNING: Unexpected VERR_PAGE_TABLE_NOT_PRESENT (%d) for page fault at %RGp error code %x (rip=%RGv)\n", rc, pvFault, uErr, pRegFrame->rip));
300 /* Some kind of inconsistency in the SMP case; it's safe to just execute the instruction again; not sure about
301 single VCPU VMs though. */
302 rc = VINF_SUCCESS;
303 }
304
305 STAM_STATS({ if (!pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution))
306 pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = &pVCpu->pgm.s.StatRZTrap0eTime2Misc; });
307 STAM_PROFILE_STOP_EX(&pVCpu->pgm.s.StatRZTrap0e, pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution), a);
308 return rc;
309}
310
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette