1 | /* $Id: PGMMap.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager, Guest Context Mappings.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_PGM
|
---|
23 | #include <VBox/dbgf.h>
|
---|
24 | #include <VBox/pgm.h>
|
---|
25 | #include "PGMInternal.h"
|
---|
26 | #include <VBox/vm.h>
|
---|
27 | #include "PGMInline.h"
|
---|
28 |
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <iprt/asm.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | /*******************************************************************************
|
---|
37 | * Internal Functions *
|
---|
38 | *******************************************************************************/
|
---|
39 | static void pgmR3MapClearPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iOldPDE);
|
---|
40 | static void pgmR3MapSetPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iNewPDE);
|
---|
41 | static int pgmR3MapIntermediateCheckOne(PVM pVM, uintptr_t uAddress, unsigned cPages, PX86PT pPTDefault, PX86PTPAE pPTPaeDefault);
|
---|
42 | static void pgmR3MapIntermediateDoOne(PVM pVM, uintptr_t uAddress, RTHCPHYS HCPhys, unsigned cPages, PX86PT pPTDefault, PX86PTPAE pPTPaeDefault);
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Creates a page table based mapping in GC.
|
---|
47 | *
|
---|
48 | * @returns VBox status code.
|
---|
49 | * @param pVM VM Handle.
|
---|
50 | * @param GCPtr Virtual Address. (Page table aligned!)
|
---|
51 | * @param cb Size of the range. Must be a 4MB aligned!
|
---|
52 | * @param fFlags PGMR3MAPPT_FLAGS_UNMAPPABLE or 0.
|
---|
53 | * @param pfnRelocate Relocation callback function.
|
---|
54 | * @param pvUser User argument to the callback.
|
---|
55 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
56 | */
|
---|
57 | VMMR3DECL(int) PGMR3MapPT(PVM pVM, RTGCPTR GCPtr, uint32_t cb, uint32_t fFlags, PFNPGMRELOCATE pfnRelocate, void *pvUser, const char *pszDesc)
|
---|
58 | {
|
---|
59 | LogFlow(("PGMR3MapPT: GCPtr=%#x cb=%d fFlags=%#x pfnRelocate=%p pvUser=%p pszDesc=%s\n", GCPtr, cb, fFlags, pfnRelocate, pvUser, pszDesc));
|
---|
60 | AssertMsg(pVM->pgm.s.pInterPD, ("Paging isn't initialized, init order problems!\n"));
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * Validate input.
|
---|
64 | */
|
---|
65 | Assert(!fFlags || fFlags == PGMR3MAPPT_FLAGS_UNMAPPABLE);
|
---|
66 | if (cb < _2M || cb > 64 * _1M)
|
---|
67 | {
|
---|
68 | AssertMsgFailed(("Serious? cb=%d\n", cb));
|
---|
69 | return VERR_INVALID_PARAMETER;
|
---|
70 | }
|
---|
71 | cb = RT_ALIGN_32(cb, _4M);
|
---|
72 | RTGCPTR GCPtrLast = GCPtr + cb - 1;
|
---|
73 |
|
---|
74 | AssertMsgReturn(GCPtrLast >= GCPtr, ("Range wraps! GCPtr=%x GCPtrLast=%x\n", GCPtr, GCPtrLast),
|
---|
75 | VERR_INVALID_PARAMETER);
|
---|
76 | AssertMsgReturn(!pVM->pgm.s.fMappingsFixed, ("Mappings are fixed! It's not possible to add new mappings at this time!\n"),
|
---|
77 | VERR_PGM_MAPPINGS_FIXED);
|
---|
78 | AssertPtrReturn(pfnRelocate, VERR_INVALID_PARAMETER);
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Find list location.
|
---|
82 | */
|
---|
83 | PPGMMAPPING pPrev = NULL;
|
---|
84 | PPGMMAPPING pCur = pVM->pgm.s.pMappingsR3;
|
---|
85 | while (pCur)
|
---|
86 | {
|
---|
87 | if (pCur->GCPtrLast >= GCPtr && pCur->GCPtr <= GCPtrLast)
|
---|
88 | {
|
---|
89 | AssertMsgFailed(("Address is already in use by %s. req %#x-%#x take %#x-%#x\n",
|
---|
90 | pCur->pszDesc, GCPtr, GCPtrLast, pCur->GCPtr, pCur->GCPtrLast));
|
---|
91 | LogRel(("VERR_PGM_MAPPING_CONFLICT: Address is already in use by %s. req %#x-%#x take %#x-%#x\n",
|
---|
92 | pCur->pszDesc, GCPtr, GCPtrLast, pCur->GCPtr, pCur->GCPtrLast));
|
---|
93 | return VERR_PGM_MAPPING_CONFLICT;
|
---|
94 | }
|
---|
95 | if (pCur->GCPtr > GCPtr)
|
---|
96 | break;
|
---|
97 | pPrev = pCur;
|
---|
98 | pCur = pCur->pNextR3;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Check for conflicts with intermediate mappings.
|
---|
103 | */
|
---|
104 | const unsigned iPageDir = GCPtr >> X86_PD_SHIFT;
|
---|
105 | const unsigned cPTs = cb >> X86_PD_SHIFT;
|
---|
106 | if (pVM->pgm.s.fFinalizedMappings)
|
---|
107 | {
|
---|
108 | for (unsigned i = 0; i < cPTs; i++)
|
---|
109 | if (pVM->pgm.s.pInterPD->a[iPageDir + i].n.u1Present)
|
---|
110 | {
|
---|
111 | AssertMsgFailed(("Address %#x is already in use by an intermediate mapping.\n", GCPtr + (i << PAGE_SHIFT)));
|
---|
112 | LogRel(("VERR_PGM_MAPPING_CONFLICT: Address %#x is already in use by an intermediate mapping.\n", GCPtr + (i << PAGE_SHIFT)));
|
---|
113 | return VERR_PGM_MAPPING_CONFLICT;
|
---|
114 | }
|
---|
115 | /** @todo AMD64: add check in PAE structures too, so we can remove all the 32-Bit paging stuff there. */
|
---|
116 | }
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Allocate and initialize the new list node.
|
---|
120 | */
|
---|
121 | PPGMMAPPING pNew;
|
---|
122 | int rc;
|
---|
123 | if (fFlags & PGMR3MAPPT_FLAGS_UNMAPPABLE)
|
---|
124 | rc = MMHyperAlloc( pVM, RT_OFFSETOF(PGMMAPPING, aPTs[cPTs]), 0, MM_TAG_PGM_MAPPINGS, (void **)&pNew);
|
---|
125 | else
|
---|
126 | rc = MMR3HyperAllocOnceNoRel(pVM, RT_OFFSETOF(PGMMAPPING, aPTs[cPTs]), 0, MM_TAG_PGM_MAPPINGS, (void **)&pNew);
|
---|
127 | if (RT_FAILURE(rc))
|
---|
128 | return rc;
|
---|
129 | pNew->GCPtr = GCPtr;
|
---|
130 | pNew->GCPtrLast = GCPtrLast;
|
---|
131 | pNew->cb = cb;
|
---|
132 | pNew->pfnRelocate = pfnRelocate;
|
---|
133 | pNew->pvUser = pvUser;
|
---|
134 | pNew->pszDesc = pszDesc;
|
---|
135 | pNew->cPTs = cPTs;
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Allocate page tables and insert them into the page directories.
|
---|
139 | * (One 32-bit PT and two PAE PTs.)
|
---|
140 | */
|
---|
141 | uint8_t *pbPTs;
|
---|
142 | if (fFlags & PGMR3MAPPT_FLAGS_UNMAPPABLE)
|
---|
143 | rc = MMHyperAlloc( pVM, PAGE_SIZE * 3 * cPTs, PAGE_SIZE, MM_TAG_PGM_MAPPINGS, (void **)&pbPTs);
|
---|
144 | else
|
---|
145 | rc = MMR3HyperAllocOnceNoRel(pVM, PAGE_SIZE * 3 * cPTs, PAGE_SIZE, MM_TAG_PGM_MAPPINGS, (void **)&pbPTs);
|
---|
146 | if (RT_FAILURE(rc))
|
---|
147 | {
|
---|
148 | MMHyperFree(pVM, pNew);
|
---|
149 | return VERR_NO_MEMORY;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Init the page tables and insert them into the page directories.
|
---|
154 | */
|
---|
155 | Log4(("PGMR3MapPT: GCPtr=%RGv cPTs=%u pbPTs=%p\n", GCPtr, cPTs, pbPTs));
|
---|
156 | for (unsigned i = 0; i < cPTs; i++)
|
---|
157 | {
|
---|
158 | /*
|
---|
159 | * 32-bit.
|
---|
160 | */
|
---|
161 | pNew->aPTs[i].pPTR3 = (PX86PT)pbPTs;
|
---|
162 | pNew->aPTs[i].pPTRC = MMHyperR3ToRC(pVM, pNew->aPTs[i].pPTR3);
|
---|
163 | pNew->aPTs[i].pPTR0 = MMHyperR3ToR0(pVM, pNew->aPTs[i].pPTR3);
|
---|
164 | pNew->aPTs[i].HCPhysPT = MMR3HyperHCVirt2HCPhys(pVM, pNew->aPTs[i].pPTR3);
|
---|
165 | pbPTs += PAGE_SIZE;
|
---|
166 | Log4(("PGMR3MapPT: i=%d: pPTR3=%RHv pPTRC=%RRv pPRTR0=%RHv HCPhysPT=%RHp\n",
|
---|
167 | i, pNew->aPTs[i].pPTR3, pNew->aPTs[i].pPTRC, pNew->aPTs[i].pPTR0, pNew->aPTs[i].HCPhysPT));
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * PAE.
|
---|
171 | */
|
---|
172 | pNew->aPTs[i].HCPhysPaePT0 = MMR3HyperHCVirt2HCPhys(pVM, pbPTs);
|
---|
173 | pNew->aPTs[i].HCPhysPaePT1 = MMR3HyperHCVirt2HCPhys(pVM, pbPTs + PAGE_SIZE);
|
---|
174 | pNew->aPTs[i].paPaePTsR3 = (PPGMSHWPTPAE)pbPTs;
|
---|
175 | pNew->aPTs[i].paPaePTsRC = MMHyperR3ToRC(pVM, pbPTs);
|
---|
176 | pNew->aPTs[i].paPaePTsR0 = MMHyperR3ToR0(pVM, pbPTs);
|
---|
177 | pbPTs += PAGE_SIZE * 2;
|
---|
178 | Log4(("PGMR3MapPT: i=%d: paPaePTsR#=%RHv paPaePTsRC=%RRv paPaePTsR#=%RHv HCPhysPaePT0=%RHp HCPhysPaePT1=%RHp\n",
|
---|
179 | i, pNew->aPTs[i].paPaePTsR3, pNew->aPTs[i].paPaePTsRC, pNew->aPTs[i].paPaePTsR0, pNew->aPTs[i].HCPhysPaePT0, pNew->aPTs[i].HCPhysPaePT1));
|
---|
180 | }
|
---|
181 | if (pVM->pgm.s.fFinalizedMappings)
|
---|
182 | pgmR3MapSetPDEs(pVM, pNew, iPageDir);
|
---|
183 | /* else PGMR3FinalizeMappings() */
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Insert the new mapping.
|
---|
187 | */
|
---|
188 | pNew->pNextR3 = pCur;
|
---|
189 | pNew->pNextRC = pCur ? MMHyperR3ToRC(pVM, pCur) : NIL_RTRCPTR;
|
---|
190 | pNew->pNextR0 = pCur ? MMHyperR3ToR0(pVM, pCur) : NIL_RTR0PTR;
|
---|
191 | if (pPrev)
|
---|
192 | {
|
---|
193 | pPrev->pNextR3 = pNew;
|
---|
194 | pPrev->pNextRC = MMHyperR3ToRC(pVM, pNew);
|
---|
195 | pPrev->pNextR0 = MMHyperR3ToR0(pVM, pNew);
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | pVM->pgm.s.pMappingsR3 = pNew;
|
---|
200 | pVM->pgm.s.pMappingsRC = MMHyperR3ToRC(pVM, pNew);
|
---|
201 | pVM->pgm.s.pMappingsR0 = MMHyperR3ToR0(pVM, pNew);
|
---|
202 | }
|
---|
203 |
|
---|
204 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
205 | {
|
---|
206 | PVMCPU pVCpu = &pVM->aCpus[i];
|
---|
207 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
208 | }
|
---|
209 | return VINF_SUCCESS;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Removes a page table based mapping.
|
---|
215 | *
|
---|
216 | * @returns VBox status code.
|
---|
217 | * @param pVM VM Handle.
|
---|
218 | * @param GCPtr Virtual Address. (Page table aligned!)
|
---|
219 | *
|
---|
220 | * @remarks Don't call this without passing PGMR3MAPPT_FLAGS_UNMAPPABLE to
|
---|
221 | * PGMR3MapPT or you'll burn in the heap.
|
---|
222 | */
|
---|
223 | VMMR3DECL(int) PGMR3UnmapPT(PVM pVM, RTGCPTR GCPtr)
|
---|
224 | {
|
---|
225 | LogFlow(("PGMR3UnmapPT: GCPtr=%#x\n", GCPtr));
|
---|
226 | AssertReturn(pVM->pgm.s.fFinalizedMappings, VERR_WRONG_ORDER);
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * Find it.
|
---|
230 | */
|
---|
231 | PPGMMAPPING pPrev = NULL;
|
---|
232 | PPGMMAPPING pCur = pVM->pgm.s.pMappingsR3;
|
---|
233 | while (pCur)
|
---|
234 | {
|
---|
235 | if (pCur->GCPtr == GCPtr)
|
---|
236 | {
|
---|
237 | /*
|
---|
238 | * Unlink it.
|
---|
239 | */
|
---|
240 | if (pPrev)
|
---|
241 | {
|
---|
242 | pPrev->pNextR3 = pCur->pNextR3;
|
---|
243 | pPrev->pNextRC = pCur->pNextRC;
|
---|
244 | pPrev->pNextR0 = pCur->pNextR0;
|
---|
245 | }
|
---|
246 | else
|
---|
247 | {
|
---|
248 | pVM->pgm.s.pMappingsR3 = pCur->pNextR3;
|
---|
249 | pVM->pgm.s.pMappingsRC = pCur->pNextRC;
|
---|
250 | pVM->pgm.s.pMappingsR0 = pCur->pNextR0;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * Free the page table memory, clear page directory entries
|
---|
255 | * and free the page tables and node memory.
|
---|
256 | */
|
---|
257 | MMHyperFree(pVM, pCur->aPTs[0].pPTR3);
|
---|
258 | if (pCur->GCPtr != NIL_RTGCPTR)
|
---|
259 | pgmR3MapClearPDEs(pVM, pCur, pCur->GCPtr >> X86_PD_SHIFT);
|
---|
260 | MMHyperFree(pVM, pCur);
|
---|
261 |
|
---|
262 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
263 | {
|
---|
264 | PVMCPU pVCpu = &pVM->aCpus[i];
|
---|
265 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
266 | }
|
---|
267 | return VINF_SUCCESS;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* done? */
|
---|
271 | if (pCur->GCPtr > GCPtr)
|
---|
272 | break;
|
---|
273 |
|
---|
274 | /* next */
|
---|
275 | pPrev = pCur;
|
---|
276 | pCur = pCur->pNextR3;
|
---|
277 | }
|
---|
278 |
|
---|
279 | AssertMsgFailed(("No mapping for %#x found!\n", GCPtr));
|
---|
280 | return VERR_INVALID_PARAMETER;
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Checks whether a range of PDEs in the intermediate
|
---|
286 | * memory context are unused.
|
---|
287 | *
|
---|
288 | * We're talking 32-bit PDEs here.
|
---|
289 | *
|
---|
290 | * @returns true/false.
|
---|
291 | * @param pVM Pointer to the shared VM structure.
|
---|
292 | * @param iPD The first PDE in the range.
|
---|
293 | * @param cPTs The number of PDEs in the range.
|
---|
294 | */
|
---|
295 | DECLINLINE(bool) pgmR3AreIntermediatePDEsUnused(PVM pVM, unsigned iPD, unsigned cPTs)
|
---|
296 | {
|
---|
297 | if (pVM->pgm.s.pInterPD->a[iPD].n.u1Present)
|
---|
298 | return false;
|
---|
299 | while (cPTs > 1)
|
---|
300 | {
|
---|
301 | iPD++;
|
---|
302 | if (pVM->pgm.s.pInterPD->a[iPD].n.u1Present)
|
---|
303 | return false;
|
---|
304 | cPTs--;
|
---|
305 | }
|
---|
306 | return true;
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Unlinks the mapping.
|
---|
312 | *
|
---|
313 | * The mapping *must* be in the list.
|
---|
314 | *
|
---|
315 | * @param pVM Pointer to the shared VM structure.
|
---|
316 | * @param pMapping The mapping to unlink.
|
---|
317 | */
|
---|
318 | static void pgmR3MapUnlink(PVM pVM, PPGMMAPPING pMapping)
|
---|
319 | {
|
---|
320 | PPGMMAPPING pAfterThis = pVM->pgm.s.pMappingsR3;
|
---|
321 | if (pAfterThis == pMapping)
|
---|
322 | {
|
---|
323 | /* head */
|
---|
324 | pVM->pgm.s.pMappingsR3 = pMapping->pNextR3;
|
---|
325 | pVM->pgm.s.pMappingsRC = pMapping->pNextRC;
|
---|
326 | pVM->pgm.s.pMappingsR0 = pMapping->pNextR0;
|
---|
327 | }
|
---|
328 | else
|
---|
329 | {
|
---|
330 | /* in the list */
|
---|
331 | while (pAfterThis->pNextR3 != pMapping)
|
---|
332 | {
|
---|
333 | pAfterThis = pAfterThis->pNextR3;
|
---|
334 | AssertReleaseReturnVoid(pAfterThis);
|
---|
335 | }
|
---|
336 |
|
---|
337 | pAfterThis->pNextR3 = pMapping->pNextR3;
|
---|
338 | pAfterThis->pNextRC = pMapping->pNextRC;
|
---|
339 | pAfterThis->pNextR0 = pMapping->pNextR0;
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Links the mapping.
|
---|
346 | *
|
---|
347 | * @param pVM Pointer to the shared VM structure.
|
---|
348 | * @param pMapping The mapping to linked.
|
---|
349 | */
|
---|
350 | static void pgmR3MapLink(PVM pVM, PPGMMAPPING pMapping)
|
---|
351 | {
|
---|
352 | /*
|
---|
353 | * Find the list location (it's sorted by GCPhys) and link it in.
|
---|
354 | */
|
---|
355 | if ( !pVM->pgm.s.pMappingsR3
|
---|
356 | || pVM->pgm.s.pMappingsR3->GCPtr > pMapping->GCPtr)
|
---|
357 | {
|
---|
358 | /* head */
|
---|
359 | pMapping->pNextR3 = pVM->pgm.s.pMappingsR3;
|
---|
360 | pMapping->pNextRC = pVM->pgm.s.pMappingsRC;
|
---|
361 | pMapping->pNextR0 = pVM->pgm.s.pMappingsR0;
|
---|
362 | pVM->pgm.s.pMappingsR3 = pMapping;
|
---|
363 | pVM->pgm.s.pMappingsRC = MMHyperR3ToRC(pVM, pMapping);
|
---|
364 | pVM->pgm.s.pMappingsR0 = MMHyperR3ToR0(pVM, pMapping);
|
---|
365 | }
|
---|
366 | else
|
---|
367 | {
|
---|
368 | /* in the list */
|
---|
369 | PPGMMAPPING pAfterThis = pVM->pgm.s.pMappingsR3;
|
---|
370 | PPGMMAPPING pBeforeThis = pAfterThis->pNextR3;
|
---|
371 | while (pBeforeThis && pBeforeThis->GCPtr <= pMapping->GCPtr)
|
---|
372 | {
|
---|
373 | pAfterThis = pBeforeThis;
|
---|
374 | pBeforeThis = pBeforeThis->pNextR3;
|
---|
375 | }
|
---|
376 |
|
---|
377 | pMapping->pNextR3 = pAfterThis->pNextR3;
|
---|
378 | pMapping->pNextRC = pAfterThis->pNextRC;
|
---|
379 | pMapping->pNextR0 = pAfterThis->pNextR0;
|
---|
380 | pAfterThis->pNextR3 = pMapping;
|
---|
381 | pAfterThis->pNextRC = MMHyperR3ToRC(pVM, pMapping);
|
---|
382 | pAfterThis->pNextR0 = MMHyperR3ToR0(pVM, pMapping);
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Finalizes the intermediate context.
|
---|
389 | *
|
---|
390 | * This is called at the end of the ring-3 init and will construct the
|
---|
391 | * intermediate paging structures, relocating all the mappings in the process.
|
---|
392 | *
|
---|
393 | * @returns VBox status code.
|
---|
394 | * @param pVM Pointer to the shared VM structure.
|
---|
395 | * @thread EMT(0)
|
---|
396 | */
|
---|
397 | VMMR3DECL(int) PGMR3FinalizeMappings(PVM pVM)
|
---|
398 | {
|
---|
399 | AssertReturn(!pVM->pgm.s.fFinalizedMappings, VERR_WRONG_ORDER);
|
---|
400 | pVM->pgm.s.fFinalizedMappings = true;
|
---|
401 |
|
---|
402 | /*
|
---|
403 | * Loop until all mappings have been finalized.
|
---|
404 | */
|
---|
405 | #if 0
|
---|
406 | unsigned iPDNext = UINT32_C(0xc0000000) >> X86_PD_SHIFT; /* makes CSAM/PATM freak out booting linux. :-/ */
|
---|
407 | #elif 0
|
---|
408 | unsigned iPDNext = MM_HYPER_AREA_ADDRESS >> X86_PD_SHIFT;
|
---|
409 | #else
|
---|
410 | unsigned iPDNext = 1 << X86_PD_SHIFT; /* no hint, map them from the top. */
|
---|
411 | #endif
|
---|
412 |
|
---|
413 | PPGMMAPPING pCur;
|
---|
414 | do
|
---|
415 | {
|
---|
416 | pCur = pVM->pgm.s.pMappingsR3;
|
---|
417 | while (pCur)
|
---|
418 | {
|
---|
419 | if (!pCur->fFinalized)
|
---|
420 | {
|
---|
421 | /*
|
---|
422 | * Find a suitable location.
|
---|
423 | */
|
---|
424 | RTGCPTR const GCPtrOld = pCur->GCPtr;
|
---|
425 | const unsigned cPTs = pCur->cPTs;
|
---|
426 | unsigned iPDNew = iPDNext;
|
---|
427 | if ( iPDNew + cPTs >= X86_PG_ENTRIES /* exclude the last PD */
|
---|
428 | || !pgmR3AreIntermediatePDEsUnused(pVM, iPDNew, cPTs)
|
---|
429 | || !pCur->pfnRelocate(pVM, GCPtrOld, (RTGCPTR)iPDNew << X86_PD_SHIFT, PGMRELOCATECALL_SUGGEST, pCur->pvUser))
|
---|
430 | {
|
---|
431 | /* No luck, just scan down from 4GB-4MB, giving up at 4MB. */
|
---|
432 | iPDNew = X86_PG_ENTRIES - cPTs - 1;
|
---|
433 | while ( iPDNew > 0
|
---|
434 | && ( !pgmR3AreIntermediatePDEsUnused(pVM, iPDNew, cPTs)
|
---|
435 | || !pCur->pfnRelocate(pVM, GCPtrOld, (RTGCPTR)iPDNew << X86_PD_SHIFT, PGMRELOCATECALL_SUGGEST, pCur->pvUser))
|
---|
436 | )
|
---|
437 | iPDNew--;
|
---|
438 | AssertLogRelReturn(iPDNew != 0, VERR_PGM_INTERMEDIATE_PAGING_CONFLICT);
|
---|
439 | }
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * Relocate it (something akin to pgmR3MapRelocate).
|
---|
443 | */
|
---|
444 | pgmR3MapSetPDEs(pVM, pCur, iPDNew);
|
---|
445 |
|
---|
446 | /* unlink the mapping, update the entry and relink it. */
|
---|
447 | pgmR3MapUnlink(pVM, pCur);
|
---|
448 |
|
---|
449 | RTGCPTR const GCPtrNew = (RTGCPTR)iPDNew << X86_PD_SHIFT;
|
---|
450 | pCur->GCPtr = GCPtrNew;
|
---|
451 | pCur->GCPtrLast = GCPtrNew + pCur->cb - 1;
|
---|
452 | pCur->fFinalized = true;
|
---|
453 |
|
---|
454 | pgmR3MapLink(pVM, pCur);
|
---|
455 |
|
---|
456 | /* Finally work the callback. */
|
---|
457 | pCur->pfnRelocate(pVM, GCPtrOld, GCPtrNew, PGMRELOCATECALL_RELOCATE, pCur->pvUser);
|
---|
458 |
|
---|
459 | /*
|
---|
460 | * The list order might have changed, start from the beginning again.
|
---|
461 | */
|
---|
462 | iPDNext = iPDNew + cPTs;
|
---|
463 | break;
|
---|
464 | }
|
---|
465 |
|
---|
466 | /* next */
|
---|
467 | pCur = pCur->pNextR3;
|
---|
468 | }
|
---|
469 | } while (pCur);
|
---|
470 |
|
---|
471 | return VINF_SUCCESS;
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Gets the size of the current guest mappings if they were to be
|
---|
477 | * put next to one another.
|
---|
478 | *
|
---|
479 | * @returns VBox status code.
|
---|
480 | * @param pVM The VM.
|
---|
481 | * @param pcb Where to store the size.
|
---|
482 | */
|
---|
483 | VMMR3DECL(int) PGMR3MappingsSize(PVM pVM, uint32_t *pcb)
|
---|
484 | {
|
---|
485 | RTGCPTR cb = 0;
|
---|
486 | for (PPGMMAPPING pCur = pVM->pgm.s.pMappingsR3; pCur; pCur = pCur->pNextR3)
|
---|
487 | cb += pCur->cb;
|
---|
488 |
|
---|
489 | *pcb = cb;
|
---|
490 | AssertReturn(*pcb == cb, VERR_NUMBER_TOO_BIG);
|
---|
491 | Log(("PGMR3MappingsSize: return %d (%#x) bytes\n", cb, cb));
|
---|
492 | return VINF_SUCCESS;
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 | /**
|
---|
497 | * Fixates the guest context mappings in a range reserved from the Guest OS.
|
---|
498 | *
|
---|
499 | * @returns VBox status code.
|
---|
500 | * @param pVM The VM.
|
---|
501 | * @param GCPtrBase The address of the reserved range of guest memory.
|
---|
502 | * @param cb The size of the range starting at GCPtrBase.
|
---|
503 | */
|
---|
504 | VMMR3DECL(int) PGMR3MappingsFix(PVM pVM, RTGCPTR GCPtrBase, uint32_t cb)
|
---|
505 | {
|
---|
506 | Log(("PGMR3MappingsFix: GCPtrBase=%RGv cb=%#x (fMappingsFixed=%RTbool fMappingsDisabled=%RTbool)\n",
|
---|
507 | GCPtrBase, cb, pVM->pgm.s.fMappingsFixed, pVM->pgm.s.fMappingsDisabled));
|
---|
508 |
|
---|
509 | /*
|
---|
510 | * Ignore the additions mapping fix call if disabled.
|
---|
511 | */
|
---|
512 | if (!pgmMapAreMappingsEnabled(&pVM->pgm.s))
|
---|
513 | {
|
---|
514 | Assert(HWACCMIsEnabled(pVM));
|
---|
515 | return VINF_SUCCESS;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /*
|
---|
519 | * Only applies to VCPU 0 as we don't support SMP guests with raw mode.
|
---|
520 | */
|
---|
521 | Assert(pVM->cCpus == 1);
|
---|
522 | PVMCPU pVCpu = &pVM->aCpus[0];
|
---|
523 |
|
---|
524 | /*
|
---|
525 | * Before we do anything we'll do a forced PD sync to try make sure any
|
---|
526 | * pending relocations because of these mappings have been resolved.
|
---|
527 | */
|
---|
528 | PGMSyncCR3(pVCpu, CPUMGetGuestCR0(pVCpu), CPUMGetGuestCR3(pVCpu), CPUMGetGuestCR4(pVCpu), true);
|
---|
529 |
|
---|
530 | return pgmR3MappingsFixInternal(pVM, GCPtrBase, cb);
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Internal worker for PGMR3MappingsFix and pgmR3Load.
|
---|
536 | *
|
---|
537 | * (This does not perform a SyncCR3 before the fixation like PGMR3MappingsFix.)
|
---|
538 | *
|
---|
539 | * @returns VBox status code.
|
---|
540 | * @param pVM The VM.
|
---|
541 | * @param GCPtrBase The address of the reserved range of guest memory.
|
---|
542 | * @param cb The size of the range starting at GCPtrBase.
|
---|
543 | */
|
---|
544 | int pgmR3MappingsFixInternal(PVM pVM, RTGCPTR GCPtrBase, uint32_t cb)
|
---|
545 | {
|
---|
546 | /*
|
---|
547 | * Check input arguments and pre-conditions.
|
---|
548 | */
|
---|
549 | AssertMsgReturn(!(GCPtrBase & X86_PAGE_4M_OFFSET_MASK), ("GCPtrBase (%#x) has to be aligned on a 4MB address!\n", GCPtrBase),
|
---|
550 | VERR_INVALID_PARAMETER);
|
---|
551 | AssertMsgReturn(cb && !(cb & X86_PAGE_4M_OFFSET_MASK), ("cb (%#x) is 0 or not aligned on a 4MB address!\n", cb),
|
---|
552 | VERR_INVALID_PARAMETER);
|
---|
553 | AssertReturn(pgmMapAreMappingsEnabled(&pVM->pgm.s), VERR_INTERNAL_ERROR_3);
|
---|
554 | AssertReturn(pVM->cCpus == 1, VERR_INTERNAL_ERROR_4);
|
---|
555 |
|
---|
556 | /*
|
---|
557 | * Check that it's not conflicting with a core code mapping in the intermediate page table.
|
---|
558 | */
|
---|
559 | unsigned iPDNew = GCPtrBase >> X86_PD_SHIFT;
|
---|
560 | unsigned i = cb >> X86_PD_SHIFT;
|
---|
561 | while (i-- > 0)
|
---|
562 | {
|
---|
563 | if (pVM->pgm.s.pInterPD->a[iPDNew + i].n.u1Present)
|
---|
564 | {
|
---|
565 | /* Check that it's not one or our mappings. */
|
---|
566 | PPGMMAPPING pCur = pVM->pgm.s.pMappingsR3;
|
---|
567 | while (pCur)
|
---|
568 | {
|
---|
569 | if (iPDNew + i - (pCur->GCPtr >> X86_PD_SHIFT) < (pCur->cb >> X86_PD_SHIFT))
|
---|
570 | break;
|
---|
571 | pCur = pCur->pNextR3;
|
---|
572 | }
|
---|
573 | if (!pCur)
|
---|
574 | {
|
---|
575 | LogRel(("PGMR3MappingsFix: Conflicts with intermediate PDE %#x (GCPtrBase=%RGv cb=%#zx). The guest should retry.\n",
|
---|
576 | iPDNew + i, GCPtrBase, cb));
|
---|
577 | return VERR_PGM_MAPPINGS_FIX_CONFLICT;
|
---|
578 | }
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 | /*
|
---|
583 | * In PAE / PAE mode, make sure we don't cross page directories.
|
---|
584 | */
|
---|
585 | PVMCPU pVCpu = &pVM->aCpus[0];
|
---|
586 | if ( ( pVCpu->pgm.s.enmGuestMode == PGMMODE_PAE
|
---|
587 | || pVCpu->pgm.s.enmGuestMode == PGMMODE_PAE_NX)
|
---|
588 | && ( pVCpu->pgm.s.enmShadowMode == PGMMODE_PAE
|
---|
589 | || pVCpu->pgm.s.enmShadowMode == PGMMODE_PAE_NX))
|
---|
590 | {
|
---|
591 | unsigned iPdptBase = GCPtrBase >> X86_PDPT_SHIFT;
|
---|
592 | unsigned iPdptLast = (GCPtrBase + cb - 1) >> X86_PDPT_SHIFT;
|
---|
593 | if (iPdptBase != iPdptLast)
|
---|
594 | {
|
---|
595 | LogRel(("PGMR3MappingsFix: Crosses PD boundary; iPdptBase=%#x iPdptLast=%#x (GCPtrBase=%RGv cb=%#zx). The guest should retry.\n",
|
---|
596 | iPdptBase, iPdptLast, GCPtrBase, cb));
|
---|
597 | return VERR_PGM_MAPPINGS_FIX_CONFLICT;
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | /*
|
---|
602 | * Loop the mappings and check that they all agree on their new locations.
|
---|
603 | */
|
---|
604 | RTGCPTR GCPtrCur = GCPtrBase;
|
---|
605 | PPGMMAPPING pCur = pVM->pgm.s.pMappingsR3;
|
---|
606 | while (pCur)
|
---|
607 | {
|
---|
608 | if (!pCur->pfnRelocate(pVM, pCur->GCPtr, GCPtrCur, PGMRELOCATECALL_SUGGEST, pCur->pvUser))
|
---|
609 | {
|
---|
610 | AssertMsgFailed(("The suggested fixed address %#x was rejected by '%s'!\n", GCPtrCur, pCur->pszDesc));
|
---|
611 | return VERR_PGM_MAPPINGS_FIX_REJECTED;
|
---|
612 | }
|
---|
613 | /* next */
|
---|
614 | GCPtrCur += pCur->cb;
|
---|
615 | pCur = pCur->pNextR3;
|
---|
616 | }
|
---|
617 | if (GCPtrCur > GCPtrBase + cb)
|
---|
618 | {
|
---|
619 | AssertMsgFailed(("cb (%#x) is less than the required range %#x!\n", cb, GCPtrCur - GCPtrBase));
|
---|
620 | return VERR_PGM_MAPPINGS_FIX_TOO_SMALL;
|
---|
621 | }
|
---|
622 |
|
---|
623 | /*
|
---|
624 | * Loop the table assigning the mappings to the passed in memory
|
---|
625 | * and call their relocator callback.
|
---|
626 | */
|
---|
627 | GCPtrCur = GCPtrBase;
|
---|
628 | pCur = pVM->pgm.s.pMappingsR3;
|
---|
629 | while (pCur)
|
---|
630 | {
|
---|
631 | RTGCPTR const GCPtrOld = pCur->GCPtr;
|
---|
632 |
|
---|
633 | /*
|
---|
634 | * Relocate the page table(s).
|
---|
635 | */
|
---|
636 | if (pCur->GCPtr != NIL_RTGCPTR)
|
---|
637 | pgmR3MapClearPDEs(pVM, pCur, GCPtrOld >> X86_PD_SHIFT);
|
---|
638 | pgmR3MapSetPDEs(pVM, pCur, GCPtrCur >> X86_PD_SHIFT);
|
---|
639 |
|
---|
640 | /*
|
---|
641 | * Update the entry.
|
---|
642 | */
|
---|
643 | pCur->GCPtr = GCPtrCur;
|
---|
644 | pCur->GCPtrLast = GCPtrCur + pCur->cb - 1;
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * Callback to execute the relocation.
|
---|
648 | */
|
---|
649 | pCur->pfnRelocate(pVM, GCPtrOld, GCPtrCur, PGMRELOCATECALL_RELOCATE, pCur->pvUser);
|
---|
650 |
|
---|
651 | /*
|
---|
652 | * Advance.
|
---|
653 | */
|
---|
654 | GCPtrCur += pCur->cb;
|
---|
655 | pCur = pCur->pNextR3;
|
---|
656 | }
|
---|
657 |
|
---|
658 | /*
|
---|
659 | * Mark the mappings as fixed at this new location and return.
|
---|
660 | */
|
---|
661 | pVM->pgm.s.fMappingsFixed = true;
|
---|
662 | pVM->pgm.s.fMappingsFixedRestored = false;
|
---|
663 | pVM->pgm.s.GCPtrMappingFixed = GCPtrBase;
|
---|
664 | pVM->pgm.s.cbMappingFixed = cb;
|
---|
665 |
|
---|
666 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
667 | {
|
---|
668 | pVM->aCpus[idCpu].pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
|
---|
669 | VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_PGM_SYNC_CR3);
|
---|
670 | }
|
---|
671 | return VINF_SUCCESS;
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | /**
|
---|
676 | * Interface for disabling the guest mappings when switching to HWACCM mode
|
---|
677 | * during VM creation and VM reset.
|
---|
678 | *
|
---|
679 | * (This doesn't touch the intermediate table!)
|
---|
680 | *
|
---|
681 | * @returns VBox status code.
|
---|
682 | * @param pVM The VM.
|
---|
683 | */
|
---|
684 | VMMR3DECL(int) PGMR3MappingsDisable(PVM pVM)
|
---|
685 | {
|
---|
686 | AssertReturn(!pVM->pgm.s.fMappingsFixed, VERR_INTERNAL_ERROR_4);
|
---|
687 | AssertReturn(!pVM->pgm.s.fMappingsFixedRestored, VERR_INTERNAL_ERROR_4);
|
---|
688 | if (pVM->pgm.s.fMappingsDisabled)
|
---|
689 | return VINF_SUCCESS;
|
---|
690 |
|
---|
691 | /*
|
---|
692 | * Deactivate (only applies to Virtual CPU #0).
|
---|
693 | */
|
---|
694 | if (pVM->aCpus[0].pgm.s.pShwPageCR3R3)
|
---|
695 | {
|
---|
696 | pgmLock(pVM); /* to avoid assertions */
|
---|
697 | int rc = pgmMapDeactivateCR3(pVM, pVM->aCpus[0].pgm.s.pShwPageCR3R3);
|
---|
698 | pgmUnlock(pVM);
|
---|
699 | AssertRCReturn(rc, rc);
|
---|
700 | }
|
---|
701 |
|
---|
702 | /*
|
---|
703 | * Mark the mappings as disabled and trigger a CR3 re-sync.
|
---|
704 | */
|
---|
705 | pVM->pgm.s.fMappingsDisabled = true;
|
---|
706 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
707 | {
|
---|
708 | pVM->aCpus[idCpu].pgm.s.fSyncFlags &= ~PGM_SYNC_MONITOR_CR3;
|
---|
709 | VMCPU_FF_SET(&pVM->aCpus[idCpu], VMCPU_FF_PGM_SYNC_CR3);
|
---|
710 | }
|
---|
711 | return VINF_SUCCESS;
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Unfixes the mappings.
|
---|
717 | *
|
---|
718 | * Unless PGMR3MappingsDisable is in effect, mapping conflict detection will be
|
---|
719 | * enabled after this call. If the mappings are fixed, a full CR3 resync will
|
---|
720 | * take place afterwards.
|
---|
721 | *
|
---|
722 | * @returns VBox status code.
|
---|
723 | * @param pVM The VM.
|
---|
724 | */
|
---|
725 | VMMR3DECL(int) PGMR3MappingsUnfix(PVM pVM)
|
---|
726 | {
|
---|
727 | Log(("PGMR3MappingsUnfix: fMappingsFixed=%RTbool fMappingsDisabled=%RTbool\n", pVM->pgm.s.fMappingsFixed, pVM->pgm.s.fMappingsDisabled));
|
---|
728 | if ( pgmMapAreMappingsEnabled(&pVM->pgm.s)
|
---|
729 | && ( pVM->pgm.s.fMappingsFixed
|
---|
730 | || pVM->pgm.s.fMappingsFixedRestored)
|
---|
731 | )
|
---|
732 | {
|
---|
733 | bool const fResyncCR3 = pVM->pgm.s.fMappingsFixed;
|
---|
734 |
|
---|
735 | pVM->pgm.s.fMappingsFixed = false;
|
---|
736 | pVM->pgm.s.fMappingsFixedRestored = false;
|
---|
737 | pVM->pgm.s.GCPtrMappingFixed = 0;
|
---|
738 | pVM->pgm.s.cbMappingFixed = 0;
|
---|
739 |
|
---|
740 | if (fResyncCR3)
|
---|
741 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
742 | VMCPU_FF_SET(&pVM->aCpus[i], VMCPU_FF_PGM_SYNC_CR3);
|
---|
743 | }
|
---|
744 | return VINF_SUCCESS;
|
---|
745 | }
|
---|
746 |
|
---|
747 |
|
---|
748 | /**
|
---|
749 | * Checks if the mappings needs re-fixing after a restore.
|
---|
750 | *
|
---|
751 | * @returns true if they need, false if not.
|
---|
752 | * @param pVM The VM handle.
|
---|
753 | */
|
---|
754 | VMMR3DECL(bool) PGMR3MappingsNeedReFixing(PVM pVM)
|
---|
755 | {
|
---|
756 | VM_ASSERT_VALID_EXT_RETURN(pVM, false);
|
---|
757 | return pVM->pgm.s.fMappingsFixedRestored;
|
---|
758 | }
|
---|
759 |
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Map pages into the intermediate context (switcher code).
|
---|
763 | *
|
---|
764 | * These pages are mapped at both the give virtual address and at the physical
|
---|
765 | * address (for identity mapping).
|
---|
766 | *
|
---|
767 | * @returns VBox status code.
|
---|
768 | * @param pVM The virtual machine.
|
---|
769 | * @param Addr Intermediate context address of the mapping.
|
---|
770 | * @param HCPhys Start of the range of physical pages. This must be entriely below 4GB!
|
---|
771 | * @param cbPages Number of bytes to map.
|
---|
772 | *
|
---|
773 | * @remark This API shall not be used to anything but mapping the switcher code.
|
---|
774 | */
|
---|
775 | VMMR3DECL(int) PGMR3MapIntermediate(PVM pVM, RTUINTPTR Addr, RTHCPHYS HCPhys, unsigned cbPages)
|
---|
776 | {
|
---|
777 | LogFlow(("PGMR3MapIntermediate: Addr=%RTptr HCPhys=%RHp cbPages=%#x\n", Addr, HCPhys, cbPages));
|
---|
778 |
|
---|
779 | /*
|
---|
780 | * Adjust input.
|
---|
781 | */
|
---|
782 | cbPages += (uint32_t)HCPhys & PAGE_OFFSET_MASK;
|
---|
783 | cbPages = RT_ALIGN(cbPages, PAGE_SIZE);
|
---|
784 | HCPhys &= X86_PTE_PAE_PG_MASK;
|
---|
785 | Addr &= PAGE_BASE_MASK;
|
---|
786 | /* We only care about the first 4GB, because on AMD64 we'll be repeating them all over the address space. */
|
---|
787 | uint32_t uAddress = (uint32_t)Addr;
|
---|
788 |
|
---|
789 | /*
|
---|
790 | * Assert input and state.
|
---|
791 | */
|
---|
792 | AssertMsg(pVM->pgm.s.offVM, ("Bad init order\n"));
|
---|
793 | AssertMsg(pVM->pgm.s.pInterPD, ("Bad init order, paging.\n"));
|
---|
794 | AssertMsg(cbPages <= (512 << PAGE_SHIFT), ("The mapping is too big %d bytes\n", cbPages));
|
---|
795 | AssertMsg(HCPhys < _4G && HCPhys + cbPages < _4G, ("Addr=%RTptr HCPhys=%RHp cbPages=%d\n", Addr, HCPhys, cbPages));
|
---|
796 | AssertReturn(!pVM->pgm.s.fFinalizedMappings, VERR_WRONG_ORDER);
|
---|
797 |
|
---|
798 | /*
|
---|
799 | * Check for internal conflicts between the virtual address and the physical address.
|
---|
800 | * A 1:1 mapping is fine, but partial overlapping is a no-no.
|
---|
801 | */
|
---|
802 | if ( uAddress != HCPhys
|
---|
803 | && ( uAddress < HCPhys
|
---|
804 | ? HCPhys - uAddress < cbPages
|
---|
805 | : uAddress - HCPhys < cbPages
|
---|
806 | )
|
---|
807 | )
|
---|
808 | AssertLogRelMsgFailedReturn(("Addr=%RTptr HCPhys=%RHp cbPages=%d\n", Addr, HCPhys, cbPages),
|
---|
809 | VERR_PGM_INTERMEDIATE_PAGING_CONFLICT);
|
---|
810 |
|
---|
811 | const unsigned cPages = cbPages >> PAGE_SHIFT;
|
---|
812 | int rc = pgmR3MapIntermediateCheckOne(pVM, uAddress, cPages, pVM->pgm.s.apInterPTs[0], pVM->pgm.s.apInterPaePTs[0]);
|
---|
813 | if (RT_FAILURE(rc))
|
---|
814 | return rc;
|
---|
815 | rc = pgmR3MapIntermediateCheckOne(pVM, (uintptr_t)HCPhys, cPages, pVM->pgm.s.apInterPTs[1], pVM->pgm.s.apInterPaePTs[1]);
|
---|
816 | if (RT_FAILURE(rc))
|
---|
817 | return rc;
|
---|
818 |
|
---|
819 | /*
|
---|
820 | * Everythings fine, do the mapping.
|
---|
821 | */
|
---|
822 | pgmR3MapIntermediateDoOne(pVM, uAddress, HCPhys, cPages, pVM->pgm.s.apInterPTs[0], pVM->pgm.s.apInterPaePTs[0]);
|
---|
823 | pgmR3MapIntermediateDoOne(pVM, (uintptr_t)HCPhys, HCPhys, cPages, pVM->pgm.s.apInterPTs[1], pVM->pgm.s.apInterPaePTs[1]);
|
---|
824 |
|
---|
825 | return VINF_SUCCESS;
|
---|
826 | }
|
---|
827 |
|
---|
828 |
|
---|
829 | /**
|
---|
830 | * Validates that there are no conflicts for this mapping into the intermediate context.
|
---|
831 | *
|
---|
832 | * @returns VBox status code.
|
---|
833 | * @param pVM VM handle.
|
---|
834 | * @param uAddress Address of the mapping.
|
---|
835 | * @param cPages Number of pages.
|
---|
836 | * @param pPTDefault Pointer to the default page table for this mapping.
|
---|
837 | * @param pPTPaeDefault Pointer to the default page table for this mapping.
|
---|
838 | */
|
---|
839 | static int pgmR3MapIntermediateCheckOne(PVM pVM, uintptr_t uAddress, unsigned cPages, PX86PT pPTDefault, PX86PTPAE pPTPaeDefault)
|
---|
840 | {
|
---|
841 | AssertMsg((uAddress >> X86_PD_SHIFT) + cPages <= 1024, ("64-bit fixme\n"));
|
---|
842 |
|
---|
843 | /*
|
---|
844 | * Check that the ranges are available.
|
---|
845 | * (This code doesn't have to be fast.)
|
---|
846 | */
|
---|
847 | while (cPages > 0)
|
---|
848 | {
|
---|
849 | /*
|
---|
850 | * 32-Bit.
|
---|
851 | */
|
---|
852 | unsigned iPDE = (uAddress >> X86_PD_SHIFT) & X86_PD_MASK;
|
---|
853 | unsigned iPTE = (uAddress >> X86_PT_SHIFT) & X86_PT_MASK;
|
---|
854 | PX86PT pPT = pPTDefault;
|
---|
855 | if (pVM->pgm.s.pInterPD->a[iPDE].u)
|
---|
856 | {
|
---|
857 | RTHCPHYS HCPhysPT = pVM->pgm.s.pInterPD->a[iPDE].u & X86_PDE_PG_MASK;
|
---|
858 | if (HCPhysPT == MMPage2Phys(pVM, pVM->pgm.s.apInterPTs[0]))
|
---|
859 | pPT = pVM->pgm.s.apInterPTs[0];
|
---|
860 | else if (HCPhysPT == MMPage2Phys(pVM, pVM->pgm.s.apInterPTs[1]))
|
---|
861 | pPT = pVM->pgm.s.apInterPTs[1];
|
---|
862 | else
|
---|
863 | {
|
---|
864 | /** @todo this must be handled with a relocation of the conflicting mapping!
|
---|
865 | * Which of course cannot be done because we're in the middle of the initialization. bad design! */
|
---|
866 | AssertLogRelMsgFailedReturn(("Conflict between core code and PGMR3Mapping(). uAddress=%RHv\n", uAddress),
|
---|
867 | VERR_PGM_INTERMEDIATE_PAGING_CONFLICT);
|
---|
868 | }
|
---|
869 | }
|
---|
870 | if (pPT->a[iPTE].u)
|
---|
871 | AssertLogRelMsgFailedReturn(("Conflict iPTE=%#x iPDE=%#x uAddress=%RHv pPT->a[iPTE].u=%RX32\n", iPTE, iPDE, uAddress, pPT->a[iPTE].u),
|
---|
872 | VERR_PGM_INTERMEDIATE_PAGING_CONFLICT);
|
---|
873 |
|
---|
874 | /*
|
---|
875 | * PAE.
|
---|
876 | */
|
---|
877 | const unsigned iPDPE= (uAddress >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
|
---|
878 | iPDE = (uAddress >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
|
---|
879 | iPTE = (uAddress >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
|
---|
880 | Assert(iPDPE < 4);
|
---|
881 | Assert(pVM->pgm.s.apInterPaePDs[iPDPE]);
|
---|
882 | PX86PTPAE pPTPae = pPTPaeDefault;
|
---|
883 | if (pVM->pgm.s.apInterPaePDs[iPDPE]->a[iPDE].u)
|
---|
884 | {
|
---|
885 | RTHCPHYS HCPhysPT = pVM->pgm.s.apInterPaePDs[iPDPE]->a[iPDE].u & X86_PDE_PAE_PG_MASK;
|
---|
886 | if (HCPhysPT == MMPage2Phys(pVM, pVM->pgm.s.apInterPaePTs[0]))
|
---|
887 | pPTPae = pVM->pgm.s.apInterPaePTs[0];
|
---|
888 | else if (HCPhysPT == MMPage2Phys(pVM, pVM->pgm.s.apInterPaePTs[0]))
|
---|
889 | pPTPae = pVM->pgm.s.apInterPaePTs[1];
|
---|
890 | else
|
---|
891 | {
|
---|
892 | /** @todo this must be handled with a relocation of the conflicting mapping!
|
---|
893 | * Which of course cannot be done because we're in the middle of the initialization. bad design! */
|
---|
894 | AssertLogRelMsgFailedReturn(("Conflict between core code and PGMR3Mapping(). uAddress=%RHv\n", uAddress),
|
---|
895 | VERR_PGM_INTERMEDIATE_PAGING_CONFLICT);
|
---|
896 | }
|
---|
897 | }
|
---|
898 | if (pPTPae->a[iPTE].u)
|
---|
899 | AssertLogRelMsgFailedReturn(("Conflict iPTE=%#x iPDE=%#x uAddress=%RHv pPTPae->a[iPTE].u=%#RX64\n", iPTE, iPDE, uAddress, pPTPae->a[iPTE].u),
|
---|
900 | VERR_PGM_INTERMEDIATE_PAGING_CONFLICT);
|
---|
901 |
|
---|
902 | /* next */
|
---|
903 | uAddress += PAGE_SIZE;
|
---|
904 | cPages--;
|
---|
905 | }
|
---|
906 |
|
---|
907 | return VINF_SUCCESS;
|
---|
908 | }
|
---|
909 |
|
---|
910 |
|
---|
911 |
|
---|
912 | /**
|
---|
913 | * Sets up the intermediate page tables for a verified mapping.
|
---|
914 | *
|
---|
915 | * @param pVM VM handle.
|
---|
916 | * @param uAddress Address of the mapping.
|
---|
917 | * @param HCPhys The physical address of the page range.
|
---|
918 | * @param cPages Number of pages.
|
---|
919 | * @param pPTDefault Pointer to the default page table for this mapping.
|
---|
920 | * @param pPTPaeDefault Pointer to the default page table for this mapping.
|
---|
921 | */
|
---|
922 | static void pgmR3MapIntermediateDoOne(PVM pVM, uintptr_t uAddress, RTHCPHYS HCPhys, unsigned cPages, PX86PT pPTDefault, PX86PTPAE pPTPaeDefault)
|
---|
923 | {
|
---|
924 | while (cPages > 0)
|
---|
925 | {
|
---|
926 | /*
|
---|
927 | * 32-Bit.
|
---|
928 | */
|
---|
929 | unsigned iPDE = (uAddress >> X86_PD_SHIFT) & X86_PD_MASK;
|
---|
930 | unsigned iPTE = (uAddress >> X86_PT_SHIFT) & X86_PT_MASK;
|
---|
931 | PX86PT pPT;
|
---|
932 | if (pVM->pgm.s.pInterPD->a[iPDE].u)
|
---|
933 | pPT = (PX86PT)MMPagePhys2Page(pVM, pVM->pgm.s.pInterPD->a[iPDE].u & X86_PDE_PG_MASK);
|
---|
934 | else
|
---|
935 | {
|
---|
936 | pVM->pgm.s.pInterPD->a[iPDE].u = X86_PDE_P | X86_PDE_A | X86_PDE_RW
|
---|
937 | | (uint32_t)MMPage2Phys(pVM, pPTDefault);
|
---|
938 | pPT = pPTDefault;
|
---|
939 | }
|
---|
940 | pPT->a[iPTE].u = X86_PTE_P | X86_PTE_RW | X86_PTE_A | X86_PTE_D | (uint32_t)HCPhys;
|
---|
941 |
|
---|
942 | /*
|
---|
943 | * PAE
|
---|
944 | */
|
---|
945 | const unsigned iPDPE= (uAddress >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
|
---|
946 | iPDE = (uAddress >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
|
---|
947 | iPTE = (uAddress >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK;
|
---|
948 | Assert(iPDPE < 4);
|
---|
949 | Assert(pVM->pgm.s.apInterPaePDs[iPDPE]);
|
---|
950 | PX86PTPAE pPTPae;
|
---|
951 | if (pVM->pgm.s.apInterPaePDs[iPDPE]->a[iPDE].u)
|
---|
952 | pPTPae = (PX86PTPAE)MMPagePhys2Page(pVM, pVM->pgm.s.apInterPaePDs[iPDPE]->a[iPDE].u & X86_PDE_PAE_PG_MASK);
|
---|
953 | else
|
---|
954 | {
|
---|
955 | pPTPae = pPTPaeDefault;
|
---|
956 | pVM->pgm.s.apInterPaePDs[iPDPE]->a[iPDE].u = X86_PDE_P | X86_PDE_A | X86_PDE_RW
|
---|
957 | | MMPage2Phys(pVM, pPTPaeDefault);
|
---|
958 | }
|
---|
959 | pPTPae->a[iPTE].u = X86_PTE_P | X86_PTE_RW | X86_PTE_A | X86_PTE_D | HCPhys;
|
---|
960 |
|
---|
961 | /* next */
|
---|
962 | cPages--;
|
---|
963 | HCPhys += PAGE_SIZE;
|
---|
964 | uAddress += PAGE_SIZE;
|
---|
965 | }
|
---|
966 | }
|
---|
967 |
|
---|
968 |
|
---|
969 | /**
|
---|
970 | * Clears all PDEs involved with the mapping in the shadow and intermediate page tables.
|
---|
971 | *
|
---|
972 | * @param pVM The VM handle.
|
---|
973 | * @param pMap Pointer to the mapping in question.
|
---|
974 | * @param iOldPDE The index of the 32-bit PDE corresponding to the base of the mapping.
|
---|
975 | */
|
---|
976 | static void pgmR3MapClearPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iOldPDE)
|
---|
977 | {
|
---|
978 | unsigned i = pMap->cPTs;
|
---|
979 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
980 | pgmLock(pVM); /* to avoid assertions */
|
---|
981 |
|
---|
982 | pgmMapClearShadowPDEs(pVM, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3), pMap, iOldPDE, false /*fDeactivateCR3*/);
|
---|
983 |
|
---|
984 | iOldPDE += i;
|
---|
985 | while (i-- > 0)
|
---|
986 | {
|
---|
987 | iOldPDE--;
|
---|
988 |
|
---|
989 | /*
|
---|
990 | * 32-bit.
|
---|
991 | */
|
---|
992 | pVM->pgm.s.pInterPD->a[iOldPDE].u = 0;
|
---|
993 |
|
---|
994 | /*
|
---|
995 | * PAE.
|
---|
996 | */
|
---|
997 | const unsigned iPD = iOldPDE / 256; /* iOldPDE * 2 / 512; iOldPDE is in 4 MB pages */
|
---|
998 | unsigned iPDE = iOldPDE * 2 % 512;
|
---|
999 | pVM->pgm.s.apInterPaePDs[iPD]->a[iPDE].u = 0;
|
---|
1000 | iPDE++;
|
---|
1001 | AssertFatal(iPDE < 512);
|
---|
1002 | pVM->pgm.s.apInterPaePDs[iPD]->a[iPDE].u = 0;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | pgmUnlock(pVM);
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 |
|
---|
1009 | /**
|
---|
1010 | * Sets all PDEs involved with the mapping in the shadow and intermediate page tables.
|
---|
1011 | *
|
---|
1012 | * @param pVM The VM handle.
|
---|
1013 | * @param pMap Pointer to the mapping in question.
|
---|
1014 | * @param iNewPDE The index of the 32-bit PDE corresponding to the base of the mapping.
|
---|
1015 | */
|
---|
1016 | static void pgmR3MapSetPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iNewPDE)
|
---|
1017 | {
|
---|
1018 | PPGM pPGM = &pVM->pgm.s;
|
---|
1019 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
1020 | pgmLock(pVM); /* to avoid assertions */
|
---|
1021 |
|
---|
1022 | Assert(!pgmMapAreMappingsEnabled(&pVM->pgm.s) || PGMGetGuestMode(pVCpu) <= PGMMODE_PAE_NX);
|
---|
1023 |
|
---|
1024 | pgmMapSetShadowPDEs(pVM, pMap, iNewPDE);
|
---|
1025 |
|
---|
1026 | /*
|
---|
1027 | * Init the page tables and insert them into the page directories.
|
---|
1028 | */
|
---|
1029 | unsigned i = pMap->cPTs;
|
---|
1030 | iNewPDE += i;
|
---|
1031 | while (i-- > 0)
|
---|
1032 | {
|
---|
1033 | iNewPDE--;
|
---|
1034 |
|
---|
1035 | /*
|
---|
1036 | * 32-bit.
|
---|
1037 | */
|
---|
1038 | X86PDE Pde;
|
---|
1039 | /* Default mapping page directory flags are read/write and supervisor; individual page attributes determine the final flags */
|
---|
1040 | Pde.u = PGM_PDFLAGS_MAPPING | X86_PDE_P | X86_PDE_A | X86_PDE_RW | X86_PDE_US | (uint32_t)pMap->aPTs[i].HCPhysPT;
|
---|
1041 | pPGM->pInterPD->a[iNewPDE] = Pde;
|
---|
1042 | /*
|
---|
1043 | * PAE.
|
---|
1044 | */
|
---|
1045 | const unsigned iPD = iNewPDE / 256;
|
---|
1046 | unsigned iPDE = iNewPDE * 2 % 512;
|
---|
1047 | X86PDEPAE PdePae0;
|
---|
1048 | PdePae0.u = PGM_PDFLAGS_MAPPING | X86_PDE_P | X86_PDE_A | X86_PDE_RW | X86_PDE_US | pMap->aPTs[i].HCPhysPaePT0;
|
---|
1049 | pPGM->apInterPaePDs[iPD]->a[iPDE] = PdePae0;
|
---|
1050 | iPDE++;
|
---|
1051 | AssertFatal(iPDE < 512);
|
---|
1052 | X86PDEPAE PdePae1;
|
---|
1053 | PdePae1.u = PGM_PDFLAGS_MAPPING | X86_PDE_P | X86_PDE_A | X86_PDE_RW | X86_PDE_US | pMap->aPTs[i].HCPhysPaePT1;
|
---|
1054 | pPGM->apInterPaePDs[iPD]->a[iPDE] = PdePae1;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | pgmUnlock(pVM);
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 |
|
---|
1061 | /**
|
---|
1062 | * Relocates a mapping to a new address.
|
---|
1063 | *
|
---|
1064 | * @param pVM VM handle.
|
---|
1065 | * @param pMapping The mapping to relocate.
|
---|
1066 | * @param GCPtrOldMapping The address of the start of the old mapping.
|
---|
1067 | * NIL_RTGCPTR if not currently mapped.
|
---|
1068 | * @param GCPtrNewMapping The address of the start of the new mapping.
|
---|
1069 | */
|
---|
1070 | static void pgmR3MapRelocate(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping, RTGCPTR GCPtrNewMapping)
|
---|
1071 | {
|
---|
1072 | Log(("PGM: Relocating %s from %RGv to %RGv\n", pMapping->pszDesc, GCPtrOldMapping, GCPtrNewMapping));
|
---|
1073 | AssertMsg(GCPtrOldMapping == pMapping->GCPtr, ("%RGv vs %RGv\n", GCPtrOldMapping, pMapping->GCPtr));
|
---|
1074 | AssertMsg((GCPtrOldMapping >> X86_PD_SHIFT) < X86_PG_ENTRIES, ("%RGv\n", GCPtrOldMapping));
|
---|
1075 | AssertMsg((GCPtrNewMapping >> X86_PD_SHIFT) < X86_PG_ENTRIES, ("%RGv\n", GCPtrOldMapping));
|
---|
1076 |
|
---|
1077 | /*
|
---|
1078 | * Relocate the page table(s).
|
---|
1079 | */
|
---|
1080 | if (GCPtrOldMapping != NIL_RTGCPTR)
|
---|
1081 | pgmR3MapClearPDEs(pVM, pMapping, GCPtrOldMapping >> X86_PD_SHIFT);
|
---|
1082 | pgmR3MapSetPDEs(pVM, pMapping, GCPtrNewMapping >> X86_PD_SHIFT);
|
---|
1083 |
|
---|
1084 | /*
|
---|
1085 | * Update and resort the mapping list.
|
---|
1086 | */
|
---|
1087 |
|
---|
1088 | /* Find previous mapping for pMapping, put result into pPrevMap. */
|
---|
1089 | PPGMMAPPING pPrevMap = NULL;
|
---|
1090 | PPGMMAPPING pCur = pVM->pgm.s.pMappingsR3;
|
---|
1091 | while (pCur && pCur != pMapping)
|
---|
1092 | {
|
---|
1093 | /* next */
|
---|
1094 | pPrevMap = pCur;
|
---|
1095 | pCur = pCur->pNextR3;
|
---|
1096 | }
|
---|
1097 | Assert(pCur);
|
---|
1098 |
|
---|
1099 | /* Find mapping which >= than pMapping. */
|
---|
1100 | RTGCPTR GCPtrNew = GCPtrNewMapping;
|
---|
1101 | PPGMMAPPING pPrev = NULL;
|
---|
1102 | pCur = pVM->pgm.s.pMappingsR3;
|
---|
1103 | while (pCur && pCur->GCPtr < GCPtrNew)
|
---|
1104 | {
|
---|
1105 | /* next */
|
---|
1106 | pPrev = pCur;
|
---|
1107 | pCur = pCur->pNextR3;
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | if (pCur != pMapping && pPrev != pMapping)
|
---|
1111 | {
|
---|
1112 | /*
|
---|
1113 | * Unlink.
|
---|
1114 | */
|
---|
1115 | if (pPrevMap)
|
---|
1116 | {
|
---|
1117 | pPrevMap->pNextR3 = pMapping->pNextR3;
|
---|
1118 | pPrevMap->pNextRC = pMapping->pNextRC;
|
---|
1119 | pPrevMap->pNextR0 = pMapping->pNextR0;
|
---|
1120 | }
|
---|
1121 | else
|
---|
1122 | {
|
---|
1123 | pVM->pgm.s.pMappingsR3 = pMapping->pNextR3;
|
---|
1124 | pVM->pgm.s.pMappingsRC = pMapping->pNextRC;
|
---|
1125 | pVM->pgm.s.pMappingsR0 = pMapping->pNextR0;
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | /*
|
---|
1129 | * Link
|
---|
1130 | */
|
---|
1131 | pMapping->pNextR3 = pCur;
|
---|
1132 | if (pPrev)
|
---|
1133 | {
|
---|
1134 | pMapping->pNextRC = pPrev->pNextRC;
|
---|
1135 | pMapping->pNextR0 = pPrev->pNextR0;
|
---|
1136 | pPrev->pNextR3 = pMapping;
|
---|
1137 | pPrev->pNextRC = MMHyperR3ToRC(pVM, pMapping);
|
---|
1138 | pPrev->pNextR0 = MMHyperR3ToR0(pVM, pMapping);
|
---|
1139 | }
|
---|
1140 | else
|
---|
1141 | {
|
---|
1142 | pMapping->pNextRC = pVM->pgm.s.pMappingsRC;
|
---|
1143 | pMapping->pNextR0 = pVM->pgm.s.pMappingsR0;
|
---|
1144 | pVM->pgm.s.pMappingsR3 = pMapping;
|
---|
1145 | pVM->pgm.s.pMappingsRC = MMHyperR3ToRC(pVM, pMapping);
|
---|
1146 | pVM->pgm.s.pMappingsR0 = MMHyperR3ToR0(pVM, pMapping);
|
---|
1147 | }
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | /*
|
---|
1151 | * Update the entry.
|
---|
1152 | */
|
---|
1153 | pMapping->GCPtr = GCPtrNew;
|
---|
1154 | pMapping->GCPtrLast = GCPtrNew + pMapping->cb - 1;
|
---|
1155 |
|
---|
1156 | /*
|
---|
1157 | * Callback to execute the relocation.
|
---|
1158 | */
|
---|
1159 | pMapping->pfnRelocate(pVM, GCPtrOldMapping, GCPtrNewMapping, PGMRELOCATECALL_RELOCATE, pMapping->pvUser);
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 |
|
---|
1163 | /**
|
---|
1164 | * Checks if a new mapping address wasn't previously used and caused a clash with guest mappings.
|
---|
1165 | *
|
---|
1166 | * @returns VBox status code.
|
---|
1167 | * @param pMapping The mapping which conflicts.
|
---|
1168 | * @param GCPtr New mapping address to try
|
---|
1169 | */
|
---|
1170 | bool pgmR3MapIsKnownConflictAddress(PPGMMAPPING pMapping, RTGCPTR GCPtr)
|
---|
1171 | {
|
---|
1172 | for (unsigned i = 0; i < RT_ELEMENTS(pMapping->aGCPtrConflicts); i++)
|
---|
1173 | {
|
---|
1174 | if (GCPtr == pMapping->aGCPtrConflicts[i])
|
---|
1175 | return true;
|
---|
1176 | }
|
---|
1177 | return false;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 |
|
---|
1181 | /**
|
---|
1182 | * Resolves a conflict between a page table based GC mapping and
|
---|
1183 | * the Guest OS page tables. (32 bits version)
|
---|
1184 | *
|
---|
1185 | * @returns VBox status code.
|
---|
1186 | * @param pVM VM Handle.
|
---|
1187 | * @param pMapping The mapping which conflicts.
|
---|
1188 | * @param pPDSrc The page directory of the guest OS.
|
---|
1189 | * @param GCPtrOldMapping The address of the start of the current mapping.
|
---|
1190 | */
|
---|
1191 | int pgmR3SyncPTResolveConflict(PVM pVM, PPGMMAPPING pMapping, PX86PD pPDSrc, RTGCPTR GCPtrOldMapping)
|
---|
1192 | {
|
---|
1193 | STAM_REL_COUNTER_INC(&pVM->pgm.s.cRelocations);
|
---|
1194 | STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
|
---|
1195 |
|
---|
1196 | /* Raw mode only which implies one VCPU. */
|
---|
1197 | Assert(pVM->cCpus == 1);
|
---|
1198 |
|
---|
1199 | pMapping->aGCPtrConflicts[pMapping->cConflicts & (PGMMAPPING_CONFLICT_MAX-1)] = GCPtrOldMapping;
|
---|
1200 | pMapping->cConflicts++;
|
---|
1201 |
|
---|
1202 | /*
|
---|
1203 | * Scan for free page directory entries.
|
---|
1204 | *
|
---|
1205 | * Note that we do not support mappings at the very end of the
|
---|
1206 | * address space since that will break our GCPtrEnd assumptions.
|
---|
1207 | */
|
---|
1208 | const unsigned cPTs = pMapping->cPTs;
|
---|
1209 | unsigned iPDNew = RT_ELEMENTS(pPDSrc->a) - cPTs; /* (+ 1 - 1) */
|
---|
1210 | while (iPDNew-- > 0)
|
---|
1211 | {
|
---|
1212 | if (pPDSrc->a[iPDNew].n.u1Present)
|
---|
1213 | continue;
|
---|
1214 |
|
---|
1215 | if (pgmR3MapIsKnownConflictAddress(pMapping, iPDNew << X86_PD_SHIFT))
|
---|
1216 | continue;
|
---|
1217 |
|
---|
1218 | if (cPTs > 1)
|
---|
1219 | {
|
---|
1220 | bool fOk = true;
|
---|
1221 | for (unsigned i = 1; fOk && i < cPTs; i++)
|
---|
1222 | if (pPDSrc->a[iPDNew + i].n.u1Present)
|
---|
1223 | fOk = false;
|
---|
1224 | if (!fOk)
|
---|
1225 | continue;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | /*
|
---|
1229 | * Check that it's not conflicting with an intermediate page table mapping.
|
---|
1230 | */
|
---|
1231 | bool fOk = true;
|
---|
1232 | unsigned i = cPTs;
|
---|
1233 | while (fOk && i-- > 0)
|
---|
1234 | fOk = !pVM->pgm.s.pInterPD->a[iPDNew + i].n.u1Present;
|
---|
1235 | if (!fOk)
|
---|
1236 | continue;
|
---|
1237 | /** @todo AMD64 should check the PAE directories and skip the 32bit stuff. */
|
---|
1238 |
|
---|
1239 | /*
|
---|
1240 | * Ask for the mapping.
|
---|
1241 | */
|
---|
1242 | RTGCPTR GCPtrNewMapping = (RTGCPTR32)iPDNew << X86_PD_SHIFT;
|
---|
1243 |
|
---|
1244 | if (pMapping->pfnRelocate(pVM, GCPtrOldMapping, GCPtrNewMapping, PGMRELOCATECALL_SUGGEST, pMapping->pvUser))
|
---|
1245 | {
|
---|
1246 | pgmR3MapRelocate(pVM, pMapping, GCPtrOldMapping, GCPtrNewMapping);
|
---|
1247 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
|
---|
1248 | return VINF_SUCCESS;
|
---|
1249 | }
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
|
---|
1253 | AssertMsgFailed(("Failed to relocate page table mapping '%s' from %#x! (cPTs=%d)\n", pMapping->pszDesc, GCPtrOldMapping, cPTs));
|
---|
1254 | return VERR_PGM_NO_HYPERVISOR_ADDRESS;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 |
|
---|
1258 | /**
|
---|
1259 | * Resolves a conflict between a page table based GC mapping and
|
---|
1260 | * the Guest OS page tables. (PAE bits version)
|
---|
1261 | *
|
---|
1262 | * @returns VBox status code.
|
---|
1263 | * @param pVM VM Handle.
|
---|
1264 | * @param pMapping The mapping which conflicts.
|
---|
1265 | * @param GCPtrOldMapping The address of the start of the current mapping.
|
---|
1266 | */
|
---|
1267 | int pgmR3SyncPTResolveConflictPAE(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping)
|
---|
1268 | {
|
---|
1269 | STAM_REL_COUNTER_INC(&pVM->pgm.s.cRelocations);
|
---|
1270 | STAM_PROFILE_START(&pVM->pgm.s.StatR3ResolveConflict, a);
|
---|
1271 |
|
---|
1272 | /* Raw mode only which implies one VCPU. */
|
---|
1273 | Assert(pVM->cCpus == 1);
|
---|
1274 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
1275 |
|
---|
1276 | pMapping->aGCPtrConflicts[pMapping->cConflicts & (PGMMAPPING_CONFLICT_MAX-1)] = GCPtrOldMapping;
|
---|
1277 | pMapping->cConflicts++;
|
---|
1278 |
|
---|
1279 | for (int iPDPTE = X86_PG_PAE_PDPE_ENTRIES - 1; iPDPTE >= 0; iPDPTE--)
|
---|
1280 | {
|
---|
1281 | unsigned iPDSrc;
|
---|
1282 | PX86PDPAE pPDSrc = pgmGstGetPaePDPtr(pVCpu, (RTGCPTR32)iPDPTE << X86_PDPT_SHIFT, &iPDSrc, NULL);
|
---|
1283 |
|
---|
1284 | /*
|
---|
1285 | * Scan for free page directory entries.
|
---|
1286 | *
|
---|
1287 | * Note that we do not support mappings at the very end of the
|
---|
1288 | * address space since that will break our GCPtrEnd assumptions.
|
---|
1289 | * Nor do we support mappings crossing page directories.
|
---|
1290 | */
|
---|
1291 | const unsigned cPTs = pMapping->cb >> X86_PD_PAE_SHIFT;
|
---|
1292 | unsigned iPDNew = RT_ELEMENTS(pPDSrc->a) - cPTs; /* (+ 1 - 1) */
|
---|
1293 |
|
---|
1294 | while (iPDNew-- > 0)
|
---|
1295 | {
|
---|
1296 | /* Ugly assumption that mappings start on a 4 MB boundary. */
|
---|
1297 | if (iPDNew & 1)
|
---|
1298 | continue;
|
---|
1299 |
|
---|
1300 | if (pgmR3MapIsKnownConflictAddress(pMapping, ((RTGCPTR32)iPDPTE << X86_PDPT_SHIFT) + (iPDNew << X86_PD_PAE_SHIFT)))
|
---|
1301 | continue;
|
---|
1302 |
|
---|
1303 | if (pPDSrc)
|
---|
1304 | {
|
---|
1305 | if (pPDSrc->a[iPDNew].n.u1Present)
|
---|
1306 | continue;
|
---|
1307 | if (cPTs > 1)
|
---|
1308 | {
|
---|
1309 | bool fOk = true;
|
---|
1310 | for (unsigned i = 1; fOk && i < cPTs; i++)
|
---|
1311 | if (pPDSrc->a[iPDNew + i].n.u1Present)
|
---|
1312 | fOk = false;
|
---|
1313 | if (!fOk)
|
---|
1314 | continue;
|
---|
1315 | }
|
---|
1316 | }
|
---|
1317 | /*
|
---|
1318 | * Check that it's not conflicting with an intermediate page table mapping.
|
---|
1319 | */
|
---|
1320 | bool fOk = true;
|
---|
1321 | unsigned i = cPTs;
|
---|
1322 | while (fOk && i-- > 0)
|
---|
1323 | fOk = !pVM->pgm.s.apInterPaePDs[iPDPTE]->a[iPDNew + i].n.u1Present;
|
---|
1324 | if (!fOk)
|
---|
1325 | continue;
|
---|
1326 |
|
---|
1327 | /*
|
---|
1328 | * Ask for the mapping.
|
---|
1329 | */
|
---|
1330 | RTGCPTR GCPtrNewMapping = ((RTGCPTR32)iPDPTE << X86_PDPT_SHIFT) + ((RTGCPTR32)iPDNew << X86_PD_PAE_SHIFT);
|
---|
1331 |
|
---|
1332 | if (pMapping->pfnRelocate(pVM, GCPtrOldMapping, GCPtrNewMapping, PGMRELOCATECALL_SUGGEST, pMapping->pvUser))
|
---|
1333 | {
|
---|
1334 | pgmR3MapRelocate(pVM, pMapping, GCPtrOldMapping, GCPtrNewMapping);
|
---|
1335 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
|
---|
1336 | return VINF_SUCCESS;
|
---|
1337 | }
|
---|
1338 | }
|
---|
1339 | }
|
---|
1340 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
|
---|
1341 | AssertMsgFailed(("Failed to relocate page table mapping '%s' from %#x! (cPTs=%d)\n", pMapping->pszDesc, GCPtrOldMapping, pMapping->cb >> X86_PD_PAE_SHIFT));
|
---|
1342 | return VERR_PGM_NO_HYPERVISOR_ADDRESS;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 |
|
---|
1346 | /**
|
---|
1347 | * Read memory from the guest mappings.
|
---|
1348 | *
|
---|
1349 | * This will use the page tables associated with the mappings to
|
---|
1350 | * read the memory. This means that not all kind of memory is readable
|
---|
1351 | * since we don't necessarily know how to convert that physical address
|
---|
1352 | * to a HC virtual one.
|
---|
1353 | *
|
---|
1354 | * @returns VBox status.
|
---|
1355 | * @param pVM VM handle.
|
---|
1356 | * @param pvDst The destination address (HC of course).
|
---|
1357 | * @param GCPtrSrc The source address (GC virtual address).
|
---|
1358 | * @param cb Number of bytes to read.
|
---|
1359 | *
|
---|
1360 | * @remarks The is indirectly for DBGF only.
|
---|
1361 | * @todo Consider renaming it to indicate it's special usage, or just
|
---|
1362 | * reimplement it in MMR3HyperReadGCVirt.
|
---|
1363 | */
|
---|
1364 | VMMR3DECL(int) PGMR3MapRead(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
|
---|
1365 | {
|
---|
1366 | /*
|
---|
1367 | * Simplicity over speed... Chop the request up into chunks
|
---|
1368 | * which don't cross pages.
|
---|
1369 | */
|
---|
1370 | if (cb + (GCPtrSrc & PAGE_OFFSET_MASK) > PAGE_SIZE)
|
---|
1371 | {
|
---|
1372 | for (;;)
|
---|
1373 | {
|
---|
1374 | size_t cbRead = RT_MIN(cb, PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK));
|
---|
1375 | int rc = PGMR3MapRead(pVM, pvDst, GCPtrSrc, cbRead);
|
---|
1376 | if (RT_FAILURE(rc))
|
---|
1377 | return rc;
|
---|
1378 | cb -= cbRead;
|
---|
1379 | if (!cb)
|
---|
1380 | break;
|
---|
1381 | pvDst = (char *)pvDst + cbRead;
|
---|
1382 | GCPtrSrc += cbRead;
|
---|
1383 | }
|
---|
1384 | return VINF_SUCCESS;
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | /*
|
---|
1388 | * Find the mapping.
|
---|
1389 | */
|
---|
1390 | PPGMMAPPING pCur = pVM->pgm.s.CTX_SUFF(pMappings);
|
---|
1391 | while (pCur)
|
---|
1392 | {
|
---|
1393 | RTGCPTR off = GCPtrSrc - pCur->GCPtr;
|
---|
1394 | if (off < pCur->cb)
|
---|
1395 | {
|
---|
1396 | if (off + cb > pCur->cb)
|
---|
1397 | {
|
---|
1398 | AssertMsgFailed(("Invalid page range %RGv LB%#x. mapping '%s' %RGv to %RGv\n",
|
---|
1399 | GCPtrSrc, cb, pCur->pszDesc, pCur->GCPtr, pCur->GCPtrLast));
|
---|
1400 | return VERR_INVALID_PARAMETER;
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | unsigned iPT = off >> X86_PD_SHIFT;
|
---|
1404 | unsigned iPTE = (off >> PAGE_SHIFT) & X86_PT_MASK;
|
---|
1405 | while (cb > 0 && iPTE < RT_ELEMENTS(CTXALLSUFF(pCur->aPTs[iPT].pPT)->a))
|
---|
1406 | {
|
---|
1407 | PCPGMSHWPTEPAE pPte = &pCur->aPTs[iPT].CTXALLSUFF(paPaePTs)[iPTE / 512].a[iPTE % 512];
|
---|
1408 | if (!PGMSHWPTEPAE_IS_P(*pPte))
|
---|
1409 | return VERR_PAGE_NOT_PRESENT;
|
---|
1410 | RTHCPHYS HCPhys = PGMSHWPTEPAE_GET_HCPHYS(*pPte);
|
---|
1411 |
|
---|
1412 | /*
|
---|
1413 | * Get the virtual page from the physical one.
|
---|
1414 | */
|
---|
1415 | void *pvPage;
|
---|
1416 | int rc = MMR3HCPhys2HCVirt(pVM, HCPhys, &pvPage);
|
---|
1417 | if (RT_FAILURE(rc))
|
---|
1418 | return rc;
|
---|
1419 |
|
---|
1420 | memcpy(pvDst, (char *)pvPage + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
|
---|
1421 | return VINF_SUCCESS;
|
---|
1422 | }
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | /* next */
|
---|
1426 | pCur = CTXALLSUFF(pCur->pNext);
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | return VERR_INVALID_POINTER;
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 |
|
---|
1433 | /**
|
---|
1434 | * Info callback for 'pgmhandlers'.
|
---|
1435 | *
|
---|
1436 | * @param pHlp The output helpers.
|
---|
1437 | * @param pszArgs The arguments. phys or virt.
|
---|
1438 | */
|
---|
1439 | DECLCALLBACK(void) pgmR3MapInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
1440 | {
|
---|
1441 | if (pVM->pgm.s.fMappingsDisabled)
|
---|
1442 | pHlp->pfnPrintf(pHlp, "\nThe mappings are DISABLED.\n");
|
---|
1443 | else if (pVM->pgm.s.fMappingsFixed)
|
---|
1444 | pHlp->pfnPrintf(pHlp, "\nThe mappings are FIXED: %RGv-%RGv\n",
|
---|
1445 | pVM->pgm.s.GCPtrMappingFixed, pVM->pgm.s.GCPtrMappingFixed + pVM->pgm.s.cbMappingFixed - 1);
|
---|
1446 | else if (pVM->pgm.s.fMappingsFixedRestored)
|
---|
1447 | pHlp->pfnPrintf(pHlp, "\nThe mappings are FLOATING-RESTORED-FIXED: %RGv-%RGv\n",
|
---|
1448 | pVM->pgm.s.GCPtrMappingFixed, pVM->pgm.s.GCPtrMappingFixed + pVM->pgm.s.cbMappingFixed - 1);
|
---|
1449 | else
|
---|
1450 | pHlp->pfnPrintf(pHlp, "\nThe mappings are FLOATING.\n");
|
---|
1451 |
|
---|
1452 | PPGMMAPPING pCur;
|
---|
1453 | for (pCur = pVM->pgm.s.pMappingsR3; pCur; pCur = pCur->pNextR3)
|
---|
1454 | {
|
---|
1455 | pHlp->pfnPrintf(pHlp, "%RGv - %RGv %s\n", pCur->GCPtr, pCur->GCPtrLast, pCur->pszDesc);
|
---|
1456 | if (pCur->cConflicts > 0)
|
---|
1457 | {
|
---|
1458 | pHlp->pfnPrintf(pHlp, " %u conflict%s: ", pCur->cConflicts, pCur->cConflicts == 1 ? "" : "s");
|
---|
1459 | uint32_t cLeft = RT_MIN(pCur->cConflicts, RT_ELEMENTS(pCur->aGCPtrConflicts));
|
---|
1460 | uint32_t i = pCur->cConflicts;
|
---|
1461 | while (cLeft-- > 0)
|
---|
1462 | {
|
---|
1463 | i = (i - 1) & (PGMMAPPING_CONFLICT_MAX - 1);
|
---|
1464 | pHlp->pfnPrintf(pHlp, cLeft ? "%RGv, " : "%RGv\n", pCur->aGCPtrConflicts[i]);
|
---|
1465 | }
|
---|
1466 | }
|
---|
1467 | }
|
---|
1468 | }
|
---|
1469 |
|
---|