VirtualBox

source: vbox/trunk/src/VBox/VMM/PGMMap.cpp@ 18891

Last change on this file since 18891 was 18747, checked in by vboxsync, 16 years ago

PGMR3MappingsUnfix: Flag CR3 resync.

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