1 | /* $Id: PGMR0.cpp 26233 2010-02-04 13:38:27Z 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 "../PGMInline.h"
|
---|
30 | #include <VBox/log.h>
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 |
|
---|
34 | RT_C_DECLS_BEGIN
|
---|
35 | #define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
|
---|
36 | #include "PGMR0Bth.h"
|
---|
37 | #undef PGM_BTH_NAME
|
---|
38 |
|
---|
39 | #define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
|
---|
40 | #include "PGMR0Bth.h"
|
---|
41 | #undef PGM_BTH_NAME
|
---|
42 |
|
---|
43 | #define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
|
---|
44 | #include "PGMR0Bth.h"
|
---|
45 | #undef PGM_BTH_NAME
|
---|
46 |
|
---|
47 | #define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
|
---|
48 | #include "PGMR0Bth.h"
|
---|
49 | #undef PGM_BTH_NAME
|
---|
50 |
|
---|
51 | RT_C_DECLS_END
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Worker function for PGMR3PhysAllocateHandyPages and pgmPhysEnsureHandyPage.
|
---|
56 | *
|
---|
57 | * @returns The following VBox status codes.
|
---|
58 | * @retval VINF_SUCCESS on success. FF cleared.
|
---|
59 | * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is set in this case.
|
---|
60 | *
|
---|
61 | * @param pVM The VM handle.
|
---|
62 | * @param pVCpu The VMCPU handle.
|
---|
63 | *
|
---|
64 | * @remarks Must be called from within the PGM critical section. The caller
|
---|
65 | * must clear the new pages.
|
---|
66 | */
|
---|
67 | VMMR0DECL(int) PGMR0PhysAllocateHandyPages(PVM pVM, PVMCPU pVCpu)
|
---|
68 | {
|
---|
69 | Assert(PDMCritSectIsOwnerEx(&pVM->pgm.s.CritSect, pVCpu->idCpu));
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * Check for error injection.
|
---|
73 | */
|
---|
74 | if (RT_UNLIKELY(pVM->pgm.s.fErrInjHandyPages))
|
---|
75 | return VERR_NO_MEMORY;
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Try allocate a full set of handy pages.
|
---|
79 | */
|
---|
80 | uint32_t iFirst = pVM->pgm.s.cHandyPages;
|
---|
81 | AssertReturn(iFirst <= RT_ELEMENTS(pVM->pgm.s.aHandyPages), VERR_INTERNAL_ERROR);
|
---|
82 | uint32_t cPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages) - iFirst;
|
---|
83 | if (!cPages)
|
---|
84 | return VINF_SUCCESS;
|
---|
85 | int rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
|
---|
86 | if (RT_SUCCESS(rc))
|
---|
87 | {
|
---|
88 | for (uint32_t i = 0; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
|
---|
89 | {
|
---|
90 | Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
|
---|
91 | Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
|
---|
92 | Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
|
---|
93 | Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
|
---|
94 | Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
|
---|
95 | }
|
---|
96 |
|
---|
97 | pVM->pgm.s.cHandyPages = RT_ELEMENTS(pVM->pgm.s.aHandyPages);
|
---|
98 | }
|
---|
99 | else if (rc != VERR_GMM_SEED_ME)
|
---|
100 | {
|
---|
101 | if ( ( rc == VERR_GMM_HIT_GLOBAL_LIMIT
|
---|
102 | || rc == VERR_GMM_HIT_VM_ACCOUNT_LIMIT)
|
---|
103 | && iFirst < PGM_HANDY_PAGES_MIN)
|
---|
104 | {
|
---|
105 |
|
---|
106 | #ifdef VBOX_STRICT
|
---|
107 | /* We're ASSUMING that GMM has updated all the entires before failing us. */
|
---|
108 | uint32_t i;
|
---|
109 | for (i = iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
|
---|
110 | {
|
---|
111 | Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
|
---|
112 | Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
|
---|
113 | Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
|
---|
114 | }
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Reduce the number of pages until we hit the minimum limit.
|
---|
119 | */
|
---|
120 | do
|
---|
121 | {
|
---|
122 | cPages >>= 2;
|
---|
123 | if (cPages + iFirst < PGM_HANDY_PAGES_MIN)
|
---|
124 | cPages = PGM_HANDY_PAGES_MIN - iFirst;
|
---|
125 | rc = GMMR0AllocateHandyPages(pVM, pVCpu->idCpu, cPages, cPages, &pVM->pgm.s.aHandyPages[iFirst]);
|
---|
126 | } while ( ( rc == VERR_GMM_HIT_GLOBAL_LIMIT
|
---|
127 | || rc == VERR_GMM_HIT_VM_ACCOUNT_LIMIT)
|
---|
128 | && cPages + iFirst > PGM_HANDY_PAGES_MIN);
|
---|
129 | if (RT_SUCCESS(rc))
|
---|
130 | {
|
---|
131 | #ifdef VBOX_STRICT
|
---|
132 | i = iFirst + cPages;
|
---|
133 | while (i-- > 0)
|
---|
134 | {
|
---|
135 | Assert(pVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
|
---|
136 | Assert(pVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
|
---|
137 | Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
|
---|
138 | Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_RTHCPHYS);
|
---|
139 | Assert(!(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
|
---|
140 | }
|
---|
141 |
|
---|
142 | for (i = cPages + iFirst; i < RT_ELEMENTS(pVM->pgm.s.aHandyPages); i++)
|
---|
143 | {
|
---|
144 | Assert(pVM->pgm.s.aHandyPages[i].idPage == NIL_GMM_PAGEID);
|
---|
145 | Assert(pVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
|
---|
146 | Assert(pVM->pgm.s.aHandyPages[i].HCPhysGCPhys == NIL_RTHCPHYS);
|
---|
147 | }
|
---|
148 | #endif
|
---|
149 |
|
---|
150 | pVM->pgm.s.cHandyPages = iFirst + cPages;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (RT_FAILURE(rc) && rc != VERR_GMM_SEED_ME)
|
---|
155 | {
|
---|
156 | LogRel(("PGMR0PhysAllocateHandyPages: rc=%Rrc iFirst=%d cPages=%d\n", rc, iFirst, cPages));
|
---|
157 | VM_FF_SET(pVM, VM_FF_PGM_NO_MEMORY);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | LogFlow(("PGMR0PhysAllocateHandyPages: cPages=%d rc=%Rrc\n", cPages, rc));
|
---|
163 | return rc;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * #PF Handler for nested paging.
|
---|
169 | *
|
---|
170 | * @returns VBox status code (appropriate for trap handling and GC return).
|
---|
171 | * @param pVM VM Handle.
|
---|
172 | * @param pVCpu VMCPU Handle.
|
---|
173 | * @param enmShwPagingMode Paging mode for the nested page tables
|
---|
174 | * @param uErr The trap error code.
|
---|
175 | * @param pRegFrame Trap register frame.
|
---|
176 | * @param pvFault The fault address.
|
---|
177 | */
|
---|
178 | VMMR0DECL(int) PGMR0Trap0eHandlerNestedPaging(PVM pVM, PVMCPU pVCpu, PGMMODE enmShwPagingMode, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPHYS pvFault)
|
---|
179 | {
|
---|
180 | int rc;
|
---|
181 |
|
---|
182 | LogFlow(("PGMTrap0eHandler: uErr=%#x pvFault=%RGp eip=%RGv\n", uErr, pvFault, (RTGCPTR)pRegFrame->rip));
|
---|
183 | STAM_PROFILE_START(&pVCpu->pgm.s.StatRZTrap0e, a);
|
---|
184 | STAM_STATS({ pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = NULL; } );
|
---|
185 |
|
---|
186 | /* AMD uses the host's paging mode; Intel has a single mode (EPT). */
|
---|
187 | 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));
|
---|
188 |
|
---|
189 | #ifdef VBOX_WITH_STATISTICS
|
---|
190 | /*
|
---|
191 | * Error code stats.
|
---|
192 | */
|
---|
193 | if (uErr & X86_TRAP_PF_US)
|
---|
194 | {
|
---|
195 | if (!(uErr & X86_TRAP_PF_P))
|
---|
196 | {
|
---|
197 | if (uErr & X86_TRAP_PF_RW)
|
---|
198 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentWrite);
|
---|
199 | else
|
---|
200 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNotPresentRead);
|
---|
201 | }
|
---|
202 | else if (uErr & X86_TRAP_PF_RW)
|
---|
203 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSWrite);
|
---|
204 | else if (uErr & X86_TRAP_PF_RSVD)
|
---|
205 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSReserved);
|
---|
206 | else if (uErr & X86_TRAP_PF_ID)
|
---|
207 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSNXE);
|
---|
208 | else
|
---|
209 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eUSRead);
|
---|
210 | }
|
---|
211 | else
|
---|
212 | { /* Supervisor */
|
---|
213 | if (!(uErr & X86_TRAP_PF_P))
|
---|
214 | {
|
---|
215 | if (uErr & X86_TRAP_PF_RW)
|
---|
216 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentWrite);
|
---|
217 | else
|
---|
218 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVNotPresentRead);
|
---|
219 | }
|
---|
220 | else if (uErr & X86_TRAP_PF_RW)
|
---|
221 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVWrite);
|
---|
222 | else if (uErr & X86_TRAP_PF_ID)
|
---|
223 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSNXE);
|
---|
224 | else if (uErr & X86_TRAP_PF_RSVD)
|
---|
225 | STAM_COUNTER_INC(&pVCpu->pgm.s.StatRZTrap0eSVReserved);
|
---|
226 | }
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Call the worker.
|
---|
231 | *
|
---|
232 | * We pretend the guest is in protected mode without paging, so we can use existing code to build the
|
---|
233 | * nested page tables.
|
---|
234 | */
|
---|
235 | bool fLockTaken = false;
|
---|
236 | switch(enmShwPagingMode)
|
---|
237 | {
|
---|
238 | case PGMMODE_32_BIT:
|
---|
239 | rc = PGM_BTH_NAME_32BIT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
|
---|
240 | break;
|
---|
241 | case PGMMODE_PAE:
|
---|
242 | case PGMMODE_PAE_NX:
|
---|
243 | rc = PGM_BTH_NAME_PAE_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
|
---|
244 | break;
|
---|
245 | case PGMMODE_AMD64:
|
---|
246 | case PGMMODE_AMD64_NX:
|
---|
247 | rc = PGM_BTH_NAME_AMD64_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
|
---|
248 | break;
|
---|
249 | case PGMMODE_EPT:
|
---|
250 | rc = PGM_BTH_NAME_EPT_PROT(Trap0eHandler)(pVCpu, uErr, pRegFrame, pvFault, &fLockTaken);
|
---|
251 | break;
|
---|
252 | default:
|
---|
253 | AssertFailed();
|
---|
254 | rc = VERR_INVALID_PARAMETER;
|
---|
255 | break;
|
---|
256 | }
|
---|
257 | if (fLockTaken)
|
---|
258 | {
|
---|
259 | Assert(PGMIsLockOwner(pVM));
|
---|
260 | pgmUnlock(pVM);
|
---|
261 | }
|
---|
262 | if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
|
---|
263 | rc = VINF_SUCCESS;
|
---|
264 | else
|
---|
265 | /* Note: hack alert for difficult to reproduce problem. */
|
---|
266 | if ( rc == VERR_PAGE_NOT_PRESENT /* SMP only ; disassembly might fail. */
|
---|
267 | || rc == VERR_PAGE_TABLE_NOT_PRESENT /* seen with UNI & SMP */
|
---|
268 | || rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT /* seen with SMP */
|
---|
269 | || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT) /* precaution */
|
---|
270 | {
|
---|
271 | 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));
|
---|
272 | /* Some kind of inconsistency in the SMP case; it's safe to just execute the instruction again; not sure about single VCPU VMs though. */
|
---|
273 | rc = VINF_SUCCESS;
|
---|
274 | }
|
---|
275 |
|
---|
276 | STAM_STATS({ if (!pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution))
|
---|
277 | pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution) = &pVCpu->pgm.s.StatRZTrap0eTime2Misc; });
|
---|
278 | STAM_PROFILE_STOP_EX(&pVCpu->pgm.s.StatRZTrap0e, pVCpu->pgm.s.CTX_SUFF(pStatTrap0eAttribution), a);
|
---|
279 | return rc;
|
---|
280 | }
|
---|
281 |
|
---|