VirtualBox

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

Last change on this file since 43667 was 43387, checked in by vboxsync, 12 years ago

VMM: HM cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 50.8 KB
Line 
1/* $Id: PGMMap.cpp 43387 2012-09-21 09:40:25Z 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/vmm/dbgf.h>
24#include <VBox/vmm/pgm.h>
25#include "PGMInternal.h"
26#include <VBox/vmm/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*******************************************************************************/
39static void pgmR3MapClearPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iOldPDE);
40static void pgmR3MapSetPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iNewPDE);
41static int pgmR3MapIntermediateCheckOne(PVM pVM, uintptr_t uAddress, unsigned cPages, PX86PT pPTDefault, PX86PTPAE pPTPaeDefault);
42static 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 Pointer to the VM.
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 */
57VMMR3DECL(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 Pointer to the VM.
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 */
223VMMR3DECL(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 VM.
292 * @param iPD The first PDE in the range.
293 * @param cPTs The number of PDEs in the range.
294 */
295DECLINLINE(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 VM.
316 * @param pMapping The mapping to unlink.
317 */
318static 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 VM.
348 * @param pMapping The mapping to linked.
349 */
350static 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 VM.
395 * @thread EMT(0)
396 */
397VMMR3DECL(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 Pointer to the VM.
481 * @param pcb Where to store the size.
482 */
483VMMR3DECL(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 Pointer to 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 */
504VMMR3DECL(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))
513 {
514 Assert(HMIsEnabled(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 Pointer to 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 */
544int 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), VERR_PGM_MAPPINGS_DISABLED);
554 AssertReturn(pVM->cCpus == 1, VERR_PGM_MAPPINGS_SMP);
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 HM 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 Pointer to the VM.
683 */
684VMMR3DECL(int) PGMR3MappingsDisable(PVM pVM)
685{
686 AssertReturn(!pVM->pgm.s.fMappingsFixed, VERR_PGM_MAPPINGS_FIXED);
687 AssertReturn(!pVM->pgm.s.fMappingsFixedRestored, VERR_PGM_MAPPINGS_FIXED);
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 Pointer to the VM.
724 */
725VMMR3DECL(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)
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 Pointer to the VM.
753 */
754VMMR3DECL(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 Pointer to the VM.
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 */
775VMMR3DECL(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 Pointer to the VM.
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 */
839static 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 uAddress=%RGv cPages=%u\n", uAddress, cPages));
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 Pointer to the VM.
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 */
922static 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 Pointer to the VM.
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 */
976static 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 Pointer to the VM.
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 */
1016static void pgmR3MapSetPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iNewPDE)
1017{
1018 PPGM pPGM = &pVM->pgm.s;
1019#ifdef VBOX_STRICT
1020 PVMCPU pVCpu = VMMGetCpu(pVM);
1021#endif
1022 pgmLock(pVM); /* to avoid assertions */
1023
1024 Assert(!pgmMapAreMappingsEnabled(pVM) || PGMGetGuestMode(pVCpu) <= PGMMODE_PAE_NX);
1025
1026 pgmMapSetShadowPDEs(pVM, pMap, iNewPDE);
1027
1028 /*
1029 * Init the page tables and insert them into the page directories.
1030 */
1031 unsigned i = pMap->cPTs;
1032 iNewPDE += i;
1033 while (i-- > 0)
1034 {
1035 iNewPDE--;
1036
1037 /*
1038 * 32-bit.
1039 */
1040 X86PDE Pde;
1041 /* Default mapping page directory flags are read/write and supervisor; individual page attributes determine the final flags */
1042 Pde.u = PGM_PDFLAGS_MAPPING | X86_PDE_P | X86_PDE_A | X86_PDE_RW | X86_PDE_US | (uint32_t)pMap->aPTs[i].HCPhysPT;
1043 pPGM->pInterPD->a[iNewPDE] = Pde;
1044 /*
1045 * PAE.
1046 */
1047 const unsigned iPD = iNewPDE / 256;
1048 unsigned iPDE = iNewPDE * 2 % 512;
1049 X86PDEPAE PdePae0;
1050 PdePae0.u = PGM_PDFLAGS_MAPPING | X86_PDE_P | X86_PDE_A | X86_PDE_RW | X86_PDE_US | pMap->aPTs[i].HCPhysPaePT0;
1051 pPGM->apInterPaePDs[iPD]->a[iPDE] = PdePae0;
1052 iPDE++;
1053 AssertFatal(iPDE < 512);
1054 X86PDEPAE PdePae1;
1055 PdePae1.u = PGM_PDFLAGS_MAPPING | X86_PDE_P | X86_PDE_A | X86_PDE_RW | X86_PDE_US | pMap->aPTs[i].HCPhysPaePT1;
1056 pPGM->apInterPaePDs[iPD]->a[iPDE] = PdePae1;
1057 }
1058
1059 pgmUnlock(pVM);
1060}
1061
1062
1063/**
1064 * Relocates a mapping to a new address.
1065 *
1066 * @param pVM Pointer to the VM.
1067 * @param pMapping The mapping to relocate.
1068 * @param GCPtrOldMapping The address of the start of the old mapping.
1069 * NIL_RTGCPTR if not currently mapped.
1070 * @param GCPtrNewMapping The address of the start of the new mapping.
1071 */
1072static void pgmR3MapRelocate(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping, RTGCPTR GCPtrNewMapping)
1073{
1074 Log(("PGM: Relocating %s from %RGv to %RGv\n", pMapping->pszDesc, GCPtrOldMapping, GCPtrNewMapping));
1075 AssertMsg(GCPtrOldMapping == pMapping->GCPtr, ("%RGv vs %RGv\n", GCPtrOldMapping, pMapping->GCPtr));
1076 AssertMsg((GCPtrOldMapping >> X86_PD_SHIFT) < X86_PG_ENTRIES, ("%RGv\n", GCPtrOldMapping));
1077 AssertMsg((GCPtrNewMapping >> X86_PD_SHIFT) < X86_PG_ENTRIES, ("%RGv\n", GCPtrOldMapping));
1078
1079 /*
1080 * Relocate the page table(s).
1081 */
1082 if (GCPtrOldMapping != NIL_RTGCPTR)
1083 pgmR3MapClearPDEs(pVM, pMapping, GCPtrOldMapping >> X86_PD_SHIFT);
1084 pgmR3MapSetPDEs(pVM, pMapping, GCPtrNewMapping >> X86_PD_SHIFT);
1085
1086 /*
1087 * Update and resort the mapping list.
1088 */
1089
1090 /* Find previous mapping for pMapping, put result into pPrevMap. */
1091 PPGMMAPPING pPrevMap = NULL;
1092 PPGMMAPPING pCur = pVM->pgm.s.pMappingsR3;
1093 while (pCur && pCur != pMapping)
1094 {
1095 /* next */
1096 pPrevMap = pCur;
1097 pCur = pCur->pNextR3;
1098 }
1099 Assert(pCur);
1100
1101 /* Find mapping which >= than pMapping. */
1102 RTGCPTR GCPtrNew = GCPtrNewMapping;
1103 PPGMMAPPING pPrev = NULL;
1104 pCur = pVM->pgm.s.pMappingsR3;
1105 while (pCur && pCur->GCPtr < GCPtrNew)
1106 {
1107 /* next */
1108 pPrev = pCur;
1109 pCur = pCur->pNextR3;
1110 }
1111
1112 if (pCur != pMapping && pPrev != pMapping)
1113 {
1114 /*
1115 * Unlink.
1116 */
1117 if (pPrevMap)
1118 {
1119 pPrevMap->pNextR3 = pMapping->pNextR3;
1120 pPrevMap->pNextRC = pMapping->pNextRC;
1121 pPrevMap->pNextR0 = pMapping->pNextR0;
1122 }
1123 else
1124 {
1125 pVM->pgm.s.pMappingsR3 = pMapping->pNextR3;
1126 pVM->pgm.s.pMappingsRC = pMapping->pNextRC;
1127 pVM->pgm.s.pMappingsR0 = pMapping->pNextR0;
1128 }
1129
1130 /*
1131 * Link
1132 */
1133 pMapping->pNextR3 = pCur;
1134 if (pPrev)
1135 {
1136 pMapping->pNextRC = pPrev->pNextRC;
1137 pMapping->pNextR0 = pPrev->pNextR0;
1138 pPrev->pNextR3 = pMapping;
1139 pPrev->pNextRC = MMHyperR3ToRC(pVM, pMapping);
1140 pPrev->pNextR0 = MMHyperR3ToR0(pVM, pMapping);
1141 }
1142 else
1143 {
1144 pMapping->pNextRC = pVM->pgm.s.pMappingsRC;
1145 pMapping->pNextR0 = pVM->pgm.s.pMappingsR0;
1146 pVM->pgm.s.pMappingsR3 = pMapping;
1147 pVM->pgm.s.pMappingsRC = MMHyperR3ToRC(pVM, pMapping);
1148 pVM->pgm.s.pMappingsR0 = MMHyperR3ToR0(pVM, pMapping);
1149 }
1150 }
1151
1152 /*
1153 * Update the entry.
1154 */
1155 pMapping->GCPtr = GCPtrNew;
1156 pMapping->GCPtrLast = GCPtrNew + pMapping->cb - 1;
1157
1158 /*
1159 * Callback to execute the relocation.
1160 */
1161 pMapping->pfnRelocate(pVM, GCPtrOldMapping, GCPtrNewMapping, PGMRELOCATECALL_RELOCATE, pMapping->pvUser);
1162}
1163
1164
1165/**
1166 * Checks if a new mapping address wasn't previously used and caused a clash with guest mappings.
1167 *
1168 * @returns VBox status code.
1169 * @param pMapping The mapping which conflicts.
1170 * @param GCPtr New mapping address to try
1171 */
1172bool pgmR3MapIsKnownConflictAddress(PPGMMAPPING pMapping, RTGCPTR GCPtr)
1173{
1174 for (unsigned i = 0; i < RT_ELEMENTS(pMapping->aGCPtrConflicts); i++)
1175 {
1176 if (GCPtr == pMapping->aGCPtrConflicts[i])
1177 return true;
1178 }
1179 return false;
1180}
1181
1182
1183/**
1184 * Resolves a conflict between a page table based GC mapping and
1185 * the Guest OS page tables. (32 bits version)
1186 *
1187 * @returns VBox status code.
1188 * @param pVM Pointer to the VM.
1189 * @param pMapping The mapping which conflicts.
1190 * @param pPDSrc The page directory of the guest OS.
1191 * @param GCPtrOldMapping The address of the start of the current mapping.
1192 */
1193int pgmR3SyncPTResolveConflict(PVM pVM, PPGMMAPPING pMapping, PX86PD pPDSrc, RTGCPTR GCPtrOldMapping)
1194{
1195 STAM_REL_COUNTER_INC(&pVM->pgm.s.cRelocations);
1196 STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
1197
1198 /* Raw mode only which implies one VCPU. */
1199 Assert(pVM->cCpus == 1);
1200
1201 pMapping->aGCPtrConflicts[pMapping->cConflicts & (PGMMAPPING_CONFLICT_MAX-1)] = GCPtrOldMapping;
1202 pMapping->cConflicts++;
1203
1204 /*
1205 * Scan for free page directory entries.
1206 *
1207 * Note that we do not support mappings at the very end of the
1208 * address space since that will break our GCPtrEnd assumptions.
1209 */
1210 const unsigned cPTs = pMapping->cPTs;
1211 unsigned iPDNew = RT_ELEMENTS(pPDSrc->a) - cPTs; /* (+ 1 - 1) */
1212 while (iPDNew-- > 0)
1213 {
1214 if (pPDSrc->a[iPDNew].n.u1Present)
1215 continue;
1216
1217 if (pgmR3MapIsKnownConflictAddress(pMapping, iPDNew << X86_PD_SHIFT))
1218 continue;
1219
1220 if (cPTs > 1)
1221 {
1222 bool fOk = true;
1223 for (unsigned i = 1; fOk && i < cPTs; i++)
1224 if (pPDSrc->a[iPDNew + i].n.u1Present)
1225 fOk = false;
1226 if (!fOk)
1227 continue;
1228 }
1229
1230 /*
1231 * Check that it's not conflicting with an intermediate page table mapping.
1232 */
1233 bool fOk = true;
1234 unsigned i = cPTs;
1235 while (fOk && i-- > 0)
1236 fOk = !pVM->pgm.s.pInterPD->a[iPDNew + i].n.u1Present;
1237 if (!fOk)
1238 continue;
1239 /** @todo AMD64 should check the PAE directories and skip the 32bit stuff. */
1240
1241 /*
1242 * Ask for the mapping.
1243 */
1244 RTGCPTR GCPtrNewMapping = (RTGCPTR32)iPDNew << X86_PD_SHIFT;
1245
1246 if (pMapping->pfnRelocate(pVM, GCPtrOldMapping, GCPtrNewMapping, PGMRELOCATECALL_SUGGEST, pMapping->pvUser))
1247 {
1248 pgmR3MapRelocate(pVM, pMapping, GCPtrOldMapping, GCPtrNewMapping);
1249 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
1250 return VINF_SUCCESS;
1251 }
1252 }
1253
1254 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
1255 AssertMsgFailed(("Failed to relocate page table mapping '%s' from %#x! (cPTs=%d)\n", pMapping->pszDesc, GCPtrOldMapping, cPTs));
1256 return VERR_PGM_NO_HYPERVISOR_ADDRESS;
1257}
1258
1259
1260/**
1261 * Resolves a conflict between a page table based GC mapping and
1262 * the Guest OS page tables. (PAE bits version)
1263 *
1264 * @returns VBox status code.
1265 * @param pVM Pointer to the VM.
1266 * @param pMapping The mapping which conflicts.
1267 * @param GCPtrOldMapping The address of the start of the current mapping.
1268 */
1269int pgmR3SyncPTResolveConflictPAE(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping)
1270{
1271 STAM_REL_COUNTER_INC(&pVM->pgm.s.cRelocations);
1272 STAM_PROFILE_START(&pVM->pgm.s.StatR3ResolveConflict, a);
1273
1274 /* Raw mode only which implies one VCPU. */
1275 Assert(pVM->cCpus == 1);
1276 PVMCPU pVCpu = VMMGetCpu(pVM);
1277
1278 pMapping->aGCPtrConflicts[pMapping->cConflicts & (PGMMAPPING_CONFLICT_MAX-1)] = GCPtrOldMapping;
1279 pMapping->cConflicts++;
1280
1281 for (int iPDPTE = X86_PG_PAE_PDPE_ENTRIES - 1; iPDPTE >= 0; iPDPTE--)
1282 {
1283 unsigned iPDSrc;
1284 PX86PDPAE pPDSrc = pgmGstGetPaePDPtr(pVCpu, (RTGCPTR32)iPDPTE << X86_PDPT_SHIFT, &iPDSrc, NULL);
1285
1286 /*
1287 * Scan for free page directory entries.
1288 *
1289 * Note that we do not support mappings at the very end of the
1290 * address space since that will break our GCPtrEnd assumptions.
1291 * Nor do we support mappings crossing page directories.
1292 */
1293 const unsigned cPTs = pMapping->cb >> X86_PD_PAE_SHIFT;
1294 unsigned iPDNew = RT_ELEMENTS(pPDSrc->a) - cPTs; /* (+ 1 - 1) */
1295
1296 while (iPDNew-- > 0)
1297 {
1298 /* Ugly assumption that mappings start on a 4 MB boundary. */
1299 if (iPDNew & 1)
1300 continue;
1301
1302 if (pgmR3MapIsKnownConflictAddress(pMapping, ((RTGCPTR32)iPDPTE << X86_PDPT_SHIFT) + (iPDNew << X86_PD_PAE_SHIFT)))
1303 continue;
1304
1305 if (pPDSrc)
1306 {
1307 if (pPDSrc->a[iPDNew].n.u1Present)
1308 continue;
1309 if (cPTs > 1)
1310 {
1311 bool fOk = true;
1312 for (unsigned i = 1; fOk && i < cPTs; i++)
1313 if (pPDSrc->a[iPDNew + i].n.u1Present)
1314 fOk = false;
1315 if (!fOk)
1316 continue;
1317 }
1318 }
1319 /*
1320 * Check that it's not conflicting with an intermediate page table mapping.
1321 */
1322 bool fOk = true;
1323 unsigned i = cPTs;
1324 while (fOk && i-- > 0)
1325 fOk = !pVM->pgm.s.apInterPaePDs[iPDPTE]->a[iPDNew + i].n.u1Present;
1326 if (!fOk)
1327 continue;
1328
1329 /*
1330 * Ask for the mapping.
1331 */
1332 RTGCPTR GCPtrNewMapping = ((RTGCPTR32)iPDPTE << X86_PDPT_SHIFT) + ((RTGCPTR32)iPDNew << X86_PD_PAE_SHIFT);
1333
1334 if (pMapping->pfnRelocate(pVM, GCPtrOldMapping, GCPtrNewMapping, PGMRELOCATECALL_SUGGEST, pMapping->pvUser))
1335 {
1336 pgmR3MapRelocate(pVM, pMapping, GCPtrOldMapping, GCPtrNewMapping);
1337 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
1338 return VINF_SUCCESS;
1339 }
1340 }
1341 }
1342 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->StatR3ResolveConflict, a);
1343 AssertMsgFailed(("Failed to relocate page table mapping '%s' from %#x! (cPTs=%d)\n", pMapping->pszDesc, GCPtrOldMapping, pMapping->cb >> X86_PD_PAE_SHIFT));
1344 return VERR_PGM_NO_HYPERVISOR_ADDRESS;
1345}
1346
1347
1348/**
1349 * Read memory from the guest mappings.
1350 *
1351 * This will use the page tables associated with the mappings to
1352 * read the memory. This means that not all kind of memory is readable
1353 * since we don't necessarily know how to convert that physical address
1354 * to a HC virtual one.
1355 *
1356 * @returns VBox status.
1357 * @param pVM Pointer to the VM.
1358 * @param pvDst The destination address (HC of course).
1359 * @param GCPtrSrc The source address (GC virtual address).
1360 * @param cb Number of bytes to read.
1361 *
1362 * @remarks The is indirectly for DBGF only.
1363 * @todo Consider renaming it to indicate it's special usage, or just
1364 * reimplement it in MMR3HyperReadGCVirt.
1365 */
1366VMMR3DECL(int) PGMR3MapRead(PVM pVM, void *pvDst, RTGCPTR GCPtrSrc, size_t cb)
1367{
1368 /*
1369 * Simplicity over speed... Chop the request up into chunks
1370 * which don't cross pages.
1371 */
1372 if (cb + (GCPtrSrc & PAGE_OFFSET_MASK) > PAGE_SIZE)
1373 {
1374 for (;;)
1375 {
1376 size_t cbRead = RT_MIN(cb, PAGE_SIZE - (GCPtrSrc & PAGE_OFFSET_MASK));
1377 int rc = PGMR3MapRead(pVM, pvDst, GCPtrSrc, cbRead);
1378 if (RT_FAILURE(rc))
1379 return rc;
1380 cb -= cbRead;
1381 if (!cb)
1382 break;
1383 pvDst = (char *)pvDst + cbRead;
1384 GCPtrSrc += cbRead;
1385 }
1386 return VINF_SUCCESS;
1387 }
1388
1389 /*
1390 * Find the mapping.
1391 */
1392 PPGMMAPPING pCur = pVM->pgm.s.CTX_SUFF(pMappings);
1393 while (pCur)
1394 {
1395 RTGCPTR off = GCPtrSrc - pCur->GCPtr;
1396 if (off < pCur->cb)
1397 {
1398 if (off + cb > pCur->cb)
1399 {
1400 AssertMsgFailed(("Invalid page range %RGv LB%#x. mapping '%s' %RGv to %RGv\n",
1401 GCPtrSrc, cb, pCur->pszDesc, pCur->GCPtr, pCur->GCPtrLast));
1402 return VERR_INVALID_PARAMETER;
1403 }
1404
1405 unsigned iPT = off >> X86_PD_SHIFT;
1406 unsigned iPTE = (off >> PAGE_SHIFT) & X86_PT_MASK;
1407 while (cb > 0 && iPTE < RT_ELEMENTS(CTXALLSUFF(pCur->aPTs[iPT].pPT)->a))
1408 {
1409 PCPGMSHWPTEPAE pPte = &pCur->aPTs[iPT].CTXALLSUFF(paPaePTs)[iPTE / 512].a[iPTE % 512];
1410 if (!PGMSHWPTEPAE_IS_P(*pPte))
1411 return VERR_PAGE_NOT_PRESENT;
1412 RTHCPHYS HCPhys = PGMSHWPTEPAE_GET_HCPHYS(*pPte);
1413
1414 /*
1415 * Get the virtual page from the physical one.
1416 */
1417 void *pvPage;
1418 int rc = MMR3HCPhys2HCVirt(pVM, HCPhys, &pvPage);
1419 if (RT_FAILURE(rc))
1420 return rc;
1421
1422 memcpy(pvDst, (char *)pvPage + (GCPtrSrc & PAGE_OFFSET_MASK), cb);
1423 return VINF_SUCCESS;
1424 }
1425 }
1426
1427 /* next */
1428 pCur = CTXALLSUFF(pCur->pNext);
1429 }
1430
1431 return VERR_INVALID_POINTER;
1432}
1433
1434
1435/**
1436 * Info callback for 'pgmhandlers'.
1437 *
1438 * @param pHlp The output helpers.
1439 * @param pszArgs The arguments. phys or virt.
1440 */
1441DECLCALLBACK(void) pgmR3MapInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1442{
1443 NOREF(pszArgs);
1444 if (pVM->pgm.s.fMappingsDisabled)
1445 pHlp->pfnPrintf(pHlp, "\nThe mappings are DISABLED.\n");
1446 else if (pVM->pgm.s.fMappingsFixed)
1447 pHlp->pfnPrintf(pHlp, "\nThe mappings are FIXED: %RGv-%RGv\n",
1448 pVM->pgm.s.GCPtrMappingFixed, pVM->pgm.s.GCPtrMappingFixed + pVM->pgm.s.cbMappingFixed - 1);
1449 else if (pVM->pgm.s.fMappingsFixedRestored)
1450 pHlp->pfnPrintf(pHlp, "\nThe mappings are FLOATING-RESTORED-FIXED: %RGv-%RGv\n",
1451 pVM->pgm.s.GCPtrMappingFixed, pVM->pgm.s.GCPtrMappingFixed + pVM->pgm.s.cbMappingFixed - 1);
1452 else
1453 pHlp->pfnPrintf(pHlp, "\nThe mappings are FLOATING.\n");
1454
1455 PPGMMAPPING pCur;
1456 for (pCur = pVM->pgm.s.pMappingsR3; pCur; pCur = pCur->pNextR3)
1457 {
1458 pHlp->pfnPrintf(pHlp, "%RGv - %RGv %s\n", pCur->GCPtr, pCur->GCPtrLast, pCur->pszDesc);
1459 if (pCur->cConflicts > 0)
1460 {
1461 pHlp->pfnPrintf(pHlp, " %u conflict%s: ", pCur->cConflicts, pCur->cConflicts == 1 ? "" : "s");
1462 uint32_t cLeft = RT_MIN(pCur->cConflicts, RT_ELEMENTS(pCur->aGCPtrConflicts));
1463 uint32_t i = pCur->cConflicts;
1464 while (cLeft-- > 0)
1465 {
1466 i = (i - 1) & (PGMMAPPING_CONFLICT_MAX - 1);
1467 pHlp->pfnPrintf(pHlp, cLeft ? "%RGv, " : "%RGv\n", pCur->aGCPtrConflicts[i]);
1468 }
1469 }
1470 }
1471}
1472
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use