VirtualBox

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

Last change on this file since 18768 was 18617, checked in by vboxsync, 15 years ago

PGM,EM: Handle out of memory situations more gracefully - part 1. New debugger commands: .pgmerror and .pgmerroroff.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1/* $Id: PGMR0.cpp 18617 2009-04-01 22:11:29Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor, Ring-0.
4 */
5
6/*
7 * Copyright (C) 2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_PGM
26#include <VBox/pgm.h>
27#include "PGMInternal.h"
28#include <VBox/vm.h>
29#include <VBox/log.h>
30#include <VBox/err.h>
31#include <iprt/assert.h>
32
33__BEGIN_DECLS
34#define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
35#include "PGMR0Bth.h"
36#undef PGM_BTH_NAME
37
38#define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
39#include "PGMR0Bth.h"
40#undef PGM_BTH_NAME
41
42#define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
43#include "PGMR0Bth.h"
44#undef PGM_BTH_NAME
45
46#define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
47#include "PGMR0Bth.h"
48#undef PGM_BTH_NAME
49
50__END_DECLS
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 *
62 * @remarks Must be called from within the PGM critical section. The caller
63 * must clear the new pages.
64 */
65VMMR0DECL(int) PGMR0PhysAllocateHandyPages(PVM pVM)
66{
67 Assert(PDMCritSectIsOwner(&pVM->pgm.s.CritSect));
68
69 /*
70 * Check for error injection.
71 */
72 if (RT_UNLIKELY(pVM->pgm.s.fErrInjHandyPages))
73 return VERR_NO_MEMORY;
74
75 /*
76 * Try allocate a full set of handy pages.
77 */
78 uint32_t iFirst = pVM->pgm.s.cHandyPages;
79 AssertReturn(iFirst <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), VERR_INTERNAL_ERROR);
80 uint32_t cPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages) - iFirst;
81 if (!cPages)
82 return VINF_SUCCESS;
83 int rc = GMMR0AllocateHandyPages(pVM, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
84 if (RT_SUCCESS(rc))
85 {
86 for (uint32_t i = 0; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
87 {
88 Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
89 Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
90 Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
91 Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
92 Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
93 }
94
95 pVM->pgm.s.cHandyPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages);
96 }
97 else if (rc != VERR_GMM_SEED_ME)
98 {
99 if ( ( rc == VERR_GMM_HIT_GLOBAL_LIMIT
100 || rc == VERR_GMM_HIT_VM_ACCOUNT_LIMIT)
101 && iFirst < PGM_HANDY_PAGES_MIN)
102 {
103
104#ifdef VBOX_STRICT
105 /* We're ASSUMING that GMM has updated all the entires before failing us. */
106 uint32_t i;
107 for (i = iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
108 {
109 Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
110 Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
111 Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
112 }
113#endif
114
115 /*
116 * Reduce the number of pages until we hit the minimum limit.
117 */
118 do
119 {
120 cPages >>= 2;
121 if (cPages + iFirst < PGM_HANDY_PAGES_MIN)
122 cPages = PGM_HANDY_PAGES_MIN - iFirst;
123 rc = GMMR0AllocateHandyPages(pVM, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
124 } while ( ( rc == VERR_GMM_HIT_GLOBAL_LIMIT
125 || rc == VERR_GMM_HIT_VM_ACCOUNT_LIMIT)
126 && cPages + iFirst > PGM_HANDY_PAGES_MIN);
127 if (RT_SUCCESS(rc))
128 {
129#ifdef VBOX_STRICT
130 i = iFirst + cPages;
131 while (i-- > 0)
132 {
133 Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
134 Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
135 Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
136 Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
137 Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
138 }
139
140 for (i = cPages + iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
141 {
142 Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
143 Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
144 Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
145 }
146#endif
147
148 pVM->pgm.s.cHandyPages = iFirst + cPages;
149 }
150 }
151
152 if (RT_FAILURE(rc) && rc != VERR_GMM_SEED_ME)
153 {
154 LogRel(("PGMR0PhysAllocateHandyPages: rc=%Rrc iFirst=%d cPages=%d\n", rc, iFirst, cPages));
155 VM_FF_SET(pVM, VM_FF_PGM_NO_MEMORY);
156 }
157 }
158
159
160 LogFlow(("PGMR0PhysAllocateHandyPages: cPages=%d rc=%Rrc\n", cPages, rc));
161 return rc;
162}
163
164
165/**
166 * #PF Handler for nested paging.
167 *
168 * @returns VBox status code (appropriate for trap handling and GC return).
169 * @param pVM VM Handle.
170 * @param enmShwPagingMode Paging mode for the nested page tables
171 * @param uErr The trap error code.
172 * @param pRegFrame Trap register frame.
173 * @param pvFault The fault address.
174 */
175VMMR0DECL(int) PGMR0Trap0eHandlerNestedPaging(PVM pVM, PGMMODE enmShwPagingMode, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPHYS pvFault)
176{
177 int rc;
178
179 LogFlow(("PGMTrap0eHandler: uErr=%#x pvFault=%RGp eip=%RGv\n", uErr, pvFault, (RTGCPTR)pRegFrame->rip));
180 STAM_PROFILE_START(&pVM->pgm.s.StatRZTrap0e, a);
181 STAM_STATS({ pVM->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = NULL; } );
182
183 /* AMD uses the host's paging mode; Intel has a single mode (EPT). */
184 AssertMsg(enmShwPagingMode == PGMMODE_32_BIT || enmShwPagingMode == PGMMODE_PAE || enmShwPagingMode == PGMMODE_PAE_NX || enmShwPagingMode == PGMMODE_AMD64 || enmShwPagingMode == PGMMODE_AMD64_NX || enmShwPagingMode == PGMMODE_EPT, ("enmShwPagingMode=%d\n", enmShwPagingMode));
185
186#ifdef VBOX_WITH_STATISTICS
187 /*
188 * Error code stats.
189 */
190 if (uErr & X86_TRAP_PF_US)
191 {
192 if (!(uErr & X86_TRAP_PF_P))
193 {
194 if (uErr & X86_TRAP_PF_RW)
195 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSNotPresentWrite);
196 else
197 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSNotPresentRead);
198 }
199 else if (uErr & X86_TRAP_PF_RW)
200 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSWrite);
201 else if (uErr & X86_TRAP_PF_RSVD)
202 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSReserved);
203 else if (uErr & X86_TRAP_PF_ID)
204 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSNXE);
205 else
206 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eUSRead);
207 }
208 else
209 { /* Supervisor */
210 if (!(uErr & X86_TRAP_PF_P))
211 {
212 if (uErr & X86_TRAP_PF_RW)
213 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSVNotPresentWrite);
214 else
215 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSVNotPresentRead);
216 }
217 else if (uErr & X86_TRAP_PF_RW)
218 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSVWrite);
219 else if (uErr & X86_TRAP_PF_ID)
220 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSNXE);
221 else if (uErr & X86_TRAP_PF_RSVD)
222 STAM_COUNTER_INC(&pVM->pgm.s.StatRZTrap0eSVReserved);
223 }
224#endif
225
226 /*
227 * Call the worker.
228 *
229 * We pretend the guest is in protected mode without paging, so we can use existing code to build the
230 * nested page tables.
231 */
232 switch(enmShwPagingMode)
233 {
234 case PGMMODE_32_BIT:
235 rc = PGM_BTH_NAME_32BIT_PROT(Trap0eHandler)(pVM, uErr, pRegFrame, pvFault);
236 break;
237 case PGMMODE_PAE:
238 case PGMMODE_PAE_NX:
239 rc = PGM_BTH_NAME_PAE_PROT(Trap0eHandler)(pVM, uErr, pRegFrame, pvFault);
240 break;
241 case PGMMODE_AMD64:
242 case PGMMODE_AMD64_NX:
243 rc = PGM_BTH_NAME_AMD64_PROT(Trap0eHandler)(pVM, uErr, pRegFrame, pvFault);
244 break;
245 case PGMMODE_EPT:
246 rc = PGM_BTH_NAME_EPT_PROT(Trap0eHandler)(pVM, uErr, pRegFrame, pvFault);
247 break;
248 default:
249 AssertFailed();
250 rc = VERR_INVALID_PARAMETER;
251 break;
252 }
253 if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
254 rc = VINF_SUCCESS;
255 STAM_STATS({ if (!pVM->pgm.s.CTX_SUFF(pStatTrap0eAttribution))
256 pVM->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = &pVM->pgm.s.StatRZTrap0eTime2Misc; });
257 STAM_PROFILE_STOP_EX(&pVM->pgm.s.StatRZTrap0e, pVM->pgm.s.CTX_SUFF(pStatTrap0eAttribution), a);
258 return rc;
259}
260
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