VirtualBox

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

Last change on this file since 25414 was 25227, checked in by vboxsync, 14 years ago

PGMMap.cpp: -Wshadow.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use