VirtualBox

source: vbox/trunk/src/VBox/VMM/MMHyper.cpp@ 33192

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

Need more space for the hypervisor heap for large memory guests

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 49.5 KB
Line 
1/* $Id: MMHyper.cpp 33192 2010-10-18 11:54:33Z vboxsync $ */
2/** @file
3 * MM - Memory Manager - Hypervisor Memory Area.
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_MM_HYPER
23#include <VBox/pgm.h>
24#include <VBox/mm.h>
25#include <VBox/dbgf.h>
26#include "MMInternal.h"
27#include <VBox/vm.h>
28#include <VBox/err.h>
29#include <VBox/param.h>
30#include <VBox/log.h>
31#include <include/internal/pgm.h>
32#include <iprt/alloc.h>
33#include <iprt/assert.h>
34#include <iprt/string.h>
35
36
37/*******************************************************************************
38* Internal Functions *
39*******************************************************************************/
40static DECLCALLBACK(bool) mmR3HyperRelocateCallback(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode, void *pvUser);
41static int mmR3HyperMap(PVM pVM, const size_t cb, const char *pszDesc, PRTGCPTR pGCPtr, PMMLOOKUPHYPER *ppLookup);
42static int mmR3HyperHeapCreate(PVM pVM, const size_t cb, PMMHYPERHEAP *ppHeap, PRTR0PTR pR0PtrHeap);
43static int mmR3HyperHeapMap(PVM pVM, PMMHYPERHEAP pHeap, PRTGCPTR ppHeapGC);
44static DECLCALLBACK(void) mmR3HyperInfoHma(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
45
46
47DECLINLINE(uint32_t) mmR3ComputeHyperHeapSize(PVM pVM, bool fCanUseLargerHeap)
48{
49 bool fHwVirtExtForced = VMMIsHwVirtExtForced(pVM);
50
51 if (pVM->cCpus > 1)
52 return _1M + pVM->cCpus * 2 * _64K;
53
54 if (fCanUseLargerHeap)
55 return 1280*_1K;
56 else
57 if (fHwVirtExtForced)
58 {
59 /* Need a bit more space for large memory guests. */
60 if (pVM->mm.s.cbRamBase >= _4G)
61 return _1M;
62 else
63 return 640 * _1K;
64 }
65 else
66 /* Size must be kept like this for saved state compatibility (only for raw mode though). */
67 return 1280*_1K;
68}
69
70
71/**
72 * Initializes the hypvervisor related MM stuff without
73 * calling down to PGM.
74 *
75 * PGM is not initialized at this point, PGM relies on
76 * the heap to initialize.
77 *
78 * @returns VBox status.
79 */
80int mmR3HyperInit(PVM pVM)
81{
82 LogFlow(("mmR3HyperInit:\n"));
83
84 /*
85 * Decide Hypervisor mapping in the guest context
86 * And setup various hypervisor area and heap parameters.
87 */
88 pVM->mm.s.pvHyperAreaGC = (RTGCPTR)MM_HYPER_AREA_ADDRESS;
89 pVM->mm.s.cbHyperArea = MM_HYPER_AREA_MAX_SIZE;
90 AssertRelease(RT_ALIGN_T(pVM->mm.s.pvHyperAreaGC, 1 << X86_PD_SHIFT, RTGCPTR) == pVM->mm.s.pvHyperAreaGC);
91 Assert(pVM->mm.s.pvHyperAreaGC < 0xff000000);
92
93 /** @todo @bugref{1865}, @bugref{3202}: Change the cbHyperHeap default
94 * depending on whether VT-x/AMD-V is enabled or not! Don't waste
95 * precious kernel space on heap for the PATM.
96 */
97 PCFGMNODE pMM = CFGMR3GetChild(CFGMR3GetRoot(pVM), "MM");
98 bool fCanUseLargerHeap = false;
99 int rc = CFGMR3QueryBoolDef(pMM, "CanUseLargerHeap", &fCanUseLargerHeap, false);
100 uint32_t cbHyperHeap = mmR3ComputeHyperHeapSize(pVM, fCanUseLargerHeap);
101 rc = CFGMR3QueryU32Def(pMM, "cbHyperHeap", &cbHyperHeap, cbHyperHeap);
102 AssertLogRelRCReturn(rc, rc);
103
104 cbHyperHeap = RT_ALIGN_32(cbHyperHeap, PAGE_SIZE);
105 LogRel(("MM: cbHyperHeap=%#x (%u)\n", cbHyperHeap, cbHyperHeap));
106
107 /*
108 * Allocate the hypervisor heap.
109 *
110 * (This must be done before we start adding memory to the
111 * hypervisor static area because lookup records are allocated from it.)
112 */
113 rc = mmR3HyperHeapCreate(pVM, cbHyperHeap, &pVM->mm.s.pHyperHeapR3, &pVM->mm.s.pHyperHeapR0);
114 if (RT_SUCCESS(rc))
115 {
116 /*
117 * Make a small head fence to fend of accidental sequential access.
118 */
119 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
120
121 /*
122 * Map the VM structure into the hypervisor space.
123 */
124 AssertRelease(pVM->cbSelf == RT_UOFFSETOF(VM, aCpus[pVM->cCpus]));
125 RTGCPTR GCPtr;
126 rc = MMR3HyperMapPages(pVM, pVM, pVM->pVMR0, RT_ALIGN_Z(pVM->cbSelf, PAGE_SIZE) >> PAGE_SHIFT, pVM->paVMPagesR3, "VM", &GCPtr);
127 if (RT_SUCCESS(rc))
128 {
129 pVM->pVMRC = (RTRCPTR)GCPtr;
130 for (VMCPUID i = 0; i < pVM->cCpus; i++)
131 pVM->aCpus[i].pVMRC = pVM->pVMRC;
132
133 /* Reserve a page for fencing. */
134 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
135
136 /*
137 * Map the heap into the hypervisor space.
138 */
139 rc = mmR3HyperHeapMap(pVM, pVM->mm.s.pHyperHeapR3, &GCPtr);
140 if (RT_SUCCESS(rc))
141 {
142 pVM->mm.s.pHyperHeapRC = (RTRCPTR)GCPtr;
143 Assert(pVM->mm.s.pHyperHeapRC == GCPtr);
144
145 /*
146 * Register info handlers.
147 */
148 DBGFR3InfoRegisterInternal(pVM, "hma", "Show the layout of the Hypervisor Memory Area.", mmR3HyperInfoHma);
149
150 LogFlow(("mmR3HyperInit: returns VINF_SUCCESS\n"));
151 return VINF_SUCCESS;
152 }
153 /* Caller will do proper cleanup. */
154 }
155 }
156
157 LogFlow(("mmR3HyperInit: returns %Rrc\n", rc));
158 return rc;
159}
160
161
162/**
163 * Cleans up the hypervisor heap.
164 *
165 * @returns VBox status.
166 */
167int mmR3HyperTerm(PVM pVM)
168{
169 if (pVM->mm.s.pHyperHeapR3)
170 PDMR3CritSectDelete(&pVM->mm.s.pHyperHeapR3->Lock);
171
172 return VINF_SUCCESS;
173}
174
175
176/**
177 * Finalizes the HMA mapping.
178 *
179 * This is called later during init, most (all) HMA allocations should be done
180 * by the time this function is called.
181 *
182 * @returns VBox status.
183 */
184VMMR3DECL(int) MMR3HyperInitFinalize(PVM pVM)
185{
186 LogFlow(("MMR3HyperInitFinalize:\n"));
187
188 /*
189 * Initialize the hyper heap critical section.
190 */
191 int rc = PDMR3CritSectInit(pVM, &pVM->mm.s.pHyperHeapR3->Lock, RT_SRC_POS, "MM-HYPER");
192 AssertRC(rc);
193
194 /*
195 * Adjust and create the HMA mapping.
196 */
197 while ((RTINT)pVM->mm.s.offHyperNextStatic + 64*_1K < (RTINT)pVM->mm.s.cbHyperArea - _4M)
198 pVM->mm.s.cbHyperArea -= _4M;
199 rc = PGMR3MapPT(pVM, pVM->mm.s.pvHyperAreaGC, pVM->mm.s.cbHyperArea, 0 /*fFlags*/,
200 mmR3HyperRelocateCallback, NULL, "Hypervisor Memory Area");
201 if (RT_FAILURE(rc))
202 return rc;
203 pVM->mm.s.fPGMInitialized = true;
204
205 /*
206 * Do all the delayed mappings.
207 */
208 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uintptr_t)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
209 for (;;)
210 {
211 RTGCPTR GCPtr = pVM->mm.s.pvHyperAreaGC + pLookup->off;
212 uint32_t cPages = pLookup->cb >> PAGE_SHIFT;
213 switch (pLookup->enmType)
214 {
215 case MMLOOKUPHYPERTYPE_LOCKED:
216 {
217 PCRTHCPHYS paHCPhysPages = pLookup->u.Locked.paHCPhysPages;
218 for (uint32_t i = 0; i < cPages; i++)
219 {
220 rc = PGMMap(pVM, GCPtr + (i << PAGE_SHIFT), paHCPhysPages[i], PAGE_SIZE, 0);
221 AssertRCReturn(rc, rc);
222 }
223 break;
224 }
225
226 case MMLOOKUPHYPERTYPE_HCPHYS:
227 rc = PGMMap(pVM, GCPtr, pLookup->u.HCPhys.HCPhys, pLookup->cb, 0);
228 break;
229
230 case MMLOOKUPHYPERTYPE_GCPHYS:
231 {
232 const RTGCPHYS GCPhys = pLookup->u.GCPhys.GCPhys;
233 const uint32_t cb = pLookup->cb;
234 for (uint32_t off = 0; off < cb; off += PAGE_SIZE)
235 {
236 RTHCPHYS HCPhys;
237 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys + off, &HCPhys);
238 if (RT_FAILURE(rc))
239 break;
240 rc = PGMMap(pVM, GCPtr + off, HCPhys, PAGE_SIZE, 0);
241 if (RT_FAILURE(rc))
242 break;
243 }
244 break;
245 }
246
247 case MMLOOKUPHYPERTYPE_MMIO2:
248 {
249 const RTGCPHYS offEnd = pLookup->u.MMIO2.off + pLookup->cb;
250 for (RTGCPHYS offCur = pLookup->u.MMIO2.off; offCur < offEnd; offCur += PAGE_SIZE)
251 {
252 RTHCPHYS HCPhys;
253 rc = PGMR3PhysMMIO2GetHCPhys(pVM, pLookup->u.MMIO2.pDevIns, pLookup->u.MMIO2.iRegion, offCur, &HCPhys);
254 if (RT_FAILURE(rc))
255 break;
256 rc = PGMMap(pVM, GCPtr + (offCur - pLookup->u.MMIO2.off), HCPhys, PAGE_SIZE, 0);
257 if (RT_FAILURE(rc))
258 break;
259 }
260 break;
261 }
262
263 case MMLOOKUPHYPERTYPE_DYNAMIC:
264 /* do nothing here since these are either fences or managed by someone else using PGM. */
265 break;
266
267 default:
268 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
269 break;
270 }
271
272 if (RT_FAILURE(rc))
273 {
274 AssertMsgFailed(("rc=%Rrc cb=%d off=%#RX32 enmType=%d pszDesc=%s\n",
275 rc, pLookup->cb, pLookup->off, pLookup->enmType, pLookup->pszDesc));
276 return rc;
277 }
278
279 /* next */
280 if (pLookup->offNext == (int32_t)NIL_OFFSET)
281 break;
282 pLookup = (PMMLOOKUPHYPER)((uintptr_t)pLookup + pLookup->offNext);
283 }
284
285 LogFlow(("MMR3HyperInitFinalize: returns VINF_SUCCESS\n"));
286 return VINF_SUCCESS;
287}
288
289
290/**
291 * Callback function which will be called when PGM is trying to find
292 * a new location for the mapping.
293 *
294 * The callback is called in two modes, 1) the check mode and 2) the relocate mode.
295 * In 1) the callback should say if it objects to a suggested new location. If it
296 * accepts the new location, it is called again for doing it's relocation.
297 *
298 *
299 * @returns true if the location is ok.
300 * @returns false if another location should be found.
301 * @param pVM The VM handle.
302 * @param GCPtrOld The old virtual address.
303 * @param GCPtrNew The new virtual address.
304 * @param enmMode Used to indicate the callback mode.
305 * @param pvUser User argument. Ignored.
306 * @remark The return value is no a failure indicator, it's an acceptance
307 * indicator. Relocation can not fail!
308 */
309static DECLCALLBACK(bool) mmR3HyperRelocateCallback(PVM pVM, RTGCPTR GCPtrOld, RTGCPTR GCPtrNew, PGMRELOCATECALL enmMode, void *pvUser)
310{
311 switch (enmMode)
312 {
313 /*
314 * Verify location - all locations are good for us.
315 */
316 case PGMRELOCATECALL_SUGGEST:
317 return true;
318
319 /*
320 * Execute the relocation.
321 */
322 case PGMRELOCATECALL_RELOCATE:
323 {
324 /*
325 * Accepted!
326 */
327 AssertMsg(GCPtrOld == pVM->mm.s.pvHyperAreaGC, ("GCPtrOld=%RGv pVM->mm.s.pvHyperAreaGC=%RGv\n", GCPtrOld, pVM->mm.s.pvHyperAreaGC));
328 Log(("Relocating the hypervisor from %RGv to %RGv\n", GCPtrOld, GCPtrNew));
329
330 /*
331 * Relocate the VM structure and ourselves.
332 */
333 RTGCINTPTR offDelta = GCPtrNew - GCPtrOld;
334 pVM->pVMRC += offDelta;
335 for (VMCPUID i = 0; i < pVM->cCpus; i++)
336 pVM->aCpus[i].pVMRC = pVM->pVMRC;
337
338 pVM->mm.s.pvHyperAreaGC += offDelta;
339 Assert(pVM->mm.s.pvHyperAreaGC < _4G);
340 pVM->mm.s.pHyperHeapRC += offDelta;
341 pVM->mm.s.pHyperHeapR3->pbHeapRC += offDelta;
342 pVM->mm.s.pHyperHeapR3->pVMRC = pVM->pVMRC;
343
344 /*
345 * Relocate the rest.
346 */
347 VMR3Relocate(pVM, offDelta);
348 return true;
349 }
350
351 default:
352 AssertMsgFailed(("Invalid relocation mode %d\n", enmMode));
353 }
354
355 return false;
356}
357
358/**
359 * Service a VMMCALLRING3_MMHYPER_LOCK call.
360 *
361 * @returns VBox status code.
362 * @param pVM The VM handle.
363 */
364VMMR3DECL(int) MMR3LockCall(PVM pVM)
365{
366 PMMHYPERHEAP pHeap = pVM->mm.s.CTX_SUFF(pHyperHeap);
367
368 int rc = PDMR3CritSectEnterEx(&pHeap->Lock, true /* fHostCall */);
369 AssertRC(rc);
370 return rc;
371}
372
373/**
374 * Maps contiguous HC physical memory into the hypervisor region in the GC.
375 *
376 * @return VBox status code.
377 *
378 * @param pVM VM handle.
379 * @param pvR3 Ring-3 address of the memory. Must be page aligned!
380 * @param pvR0 Optional ring-0 address of the memory.
381 * @param HCPhys Host context physical address of the memory to be
382 * mapped. Must be page aligned!
383 * @param cb Size of the memory. Will be rounded up to nearest page.
384 * @param pszDesc Description.
385 * @param pGCPtr Where to store the GC address.
386 */
387VMMR3DECL(int) MMR3HyperMapHCPhys(PVM pVM, void *pvR3, RTR0PTR pvR0, RTHCPHYS HCPhys, size_t cb, const char *pszDesc, PRTGCPTR pGCPtr)
388{
389 LogFlow(("MMR3HyperMapHCPhys: pvR3=%p pvR0=%p HCPhys=%RHp cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", pvR3, pvR0, HCPhys, (int)cb, pszDesc, pszDesc, pGCPtr));
390
391 /*
392 * Validate input.
393 */
394 AssertReturn(RT_ALIGN_P(pvR3, PAGE_SIZE) == pvR3, VERR_INVALID_PARAMETER);
395 AssertReturn(RT_ALIGN_T(pvR0, PAGE_SIZE, RTR0PTR) == pvR0, VERR_INVALID_PARAMETER);
396 AssertReturn(RT_ALIGN_T(HCPhys, PAGE_SIZE, RTHCPHYS) == HCPhys, VERR_INVALID_PARAMETER);
397 AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
398
399 /*
400 * Add the memory to the hypervisor area.
401 */
402 uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
403 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
404 RTGCPTR GCPtr;
405 PMMLOOKUPHYPER pLookup;
406 int rc = mmR3HyperMap(pVM, cbAligned, pszDesc, &GCPtr, &pLookup);
407 if (RT_SUCCESS(rc))
408 {
409 pLookup->enmType = MMLOOKUPHYPERTYPE_HCPHYS;
410 pLookup->u.HCPhys.pvR3 = pvR3;
411 pLookup->u.HCPhys.pvR0 = pvR0;
412 pLookup->u.HCPhys.HCPhys = HCPhys;
413
414 /*
415 * Update the page table.
416 */
417 if (pVM->mm.s.fPGMInitialized)
418 rc = PGMMap(pVM, GCPtr, HCPhys, cbAligned, 0);
419 if (RT_SUCCESS(rc))
420 *pGCPtr = GCPtr;
421 }
422 return rc;
423}
424
425
426/**
427 * Maps contiguous GC physical memory into the hypervisor region in the GC.
428 *
429 * @return VBox status code.
430 *
431 * @param pVM VM handle.
432 * @param GCPhys Guest context physical address of the memory to be mapped. Must be page aligned!
433 * @param cb Size of the memory. Will be rounded up to nearest page.
434 * @param pszDesc Mapping description.
435 * @param pGCPtr Where to store the GC address.
436 */
437VMMR3DECL(int) MMR3HyperMapGCPhys(PVM pVM, RTGCPHYS GCPhys, size_t cb, const char *pszDesc, PRTGCPTR pGCPtr)
438{
439 LogFlow(("MMR3HyperMapGCPhys: GCPhys=%RGp cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", GCPhys, (int)cb, pszDesc, pszDesc, pGCPtr));
440
441 /*
442 * Validate input.
443 */
444 AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
445 AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
446
447 /*
448 * Add the memory to the hypervisor area.
449 */
450 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
451 RTGCPTR GCPtr;
452 PMMLOOKUPHYPER pLookup;
453 int rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
454 if (RT_SUCCESS(rc))
455 {
456 pLookup->enmType = MMLOOKUPHYPERTYPE_GCPHYS;
457 pLookup->u.GCPhys.GCPhys = GCPhys;
458
459 /*
460 * Update the page table.
461 */
462 for (unsigned off = 0; off < cb; off += PAGE_SIZE)
463 {
464 RTHCPHYS HCPhys;
465 rc = PGMPhysGCPhys2HCPhys(pVM, GCPhys + off, &HCPhys);
466 AssertRC(rc);
467 if (RT_FAILURE(rc))
468 {
469 AssertMsgFailed(("rc=%Rrc GCPhys=%RGp off=%#x %s\n", rc, GCPhys, off, pszDesc));
470 break;
471 }
472 if (pVM->mm.s.fPGMInitialized)
473 {
474 rc = PGMMap(pVM, GCPtr + off, HCPhys, PAGE_SIZE, 0);
475 AssertRC(rc);
476 if (RT_FAILURE(rc))
477 {
478 AssertMsgFailed(("rc=%Rrc GCPhys=%RGp off=%#x %s\n", rc, GCPhys, off, pszDesc));
479 break;
480 }
481 }
482 }
483
484 if (RT_SUCCESS(rc) && pGCPtr)
485 *pGCPtr = GCPtr;
486 }
487 return rc;
488}
489
490
491/**
492 * Maps a portion of an MMIO2 region into the hypervisor region.
493 *
494 * Callers of this API must never deregister the MMIO2 region before the
495 * VM is powered off. If this becomes a requirement MMR3HyperUnmapMMIO2
496 * API will be needed to perform cleanups.
497 *
498 * @return VBox status code.
499 *
500 * @param pVM Pointer to the shared VM structure.
501 * @param pDevIns The device owning the MMIO2 memory.
502 * @param iRegion The region.
503 * @param off The offset into the region. Will be rounded down to closest page boundrary.
504 * @param cb The number of bytes to map. Will be rounded up to the closest page boundrary.
505 * @param pszDesc Mapping description.
506 * @param pRCPtr Where to store the RC address.
507 */
508VMMR3DECL(int) MMR3HyperMapMMIO2(PVM pVM, PPDMDEVINS pDevIns, uint32_t iRegion, RTGCPHYS off, RTGCPHYS cb,
509 const char *pszDesc, PRTRCPTR pRCPtr)
510{
511 LogFlow(("MMR3HyperMapMMIO2: pDevIns=%p iRegion=%#x off=%RGp cb=%RGp pszDesc=%p:{%s} pRCPtr=%p\n",
512 pDevIns, iRegion, off, cb, pszDesc, pszDesc, pRCPtr));
513 int rc;
514
515 /*
516 * Validate input.
517 */
518 AssertReturn(pszDesc && *pszDesc, VERR_INVALID_PARAMETER);
519 AssertReturn(off + cb > off, VERR_INVALID_PARAMETER);
520 uint32_t const offPage = off & PAGE_OFFSET_MASK;
521 off &= ~(RTGCPHYS)PAGE_OFFSET_MASK;
522 cb += offPage;
523 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
524 const RTGCPHYS offEnd = off + cb;
525 AssertReturn(offEnd > off, VERR_INVALID_PARAMETER);
526 for (RTGCPHYS offCur = off; offCur < offEnd; offCur += PAGE_SIZE)
527 {
528 RTHCPHYS HCPhys;
529 rc = PGMR3PhysMMIO2GetHCPhys(pVM, pDevIns, iRegion, offCur, &HCPhys);
530 AssertMsgRCReturn(rc, ("rc=%Rrc - iRegion=%d off=%RGp\n", rc, iRegion, off), rc);
531 }
532
533 /*
534 * Add the memory to the hypervisor area.
535 */
536 RTGCPTR GCPtr;
537 PMMLOOKUPHYPER pLookup;
538 rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
539 if (RT_SUCCESS(rc))
540 {
541 pLookup->enmType = MMLOOKUPHYPERTYPE_MMIO2;
542 pLookup->u.MMIO2.pDevIns = pDevIns;
543 pLookup->u.MMIO2.iRegion = iRegion;
544 pLookup->u.MMIO2.off = off;
545
546 /*
547 * Update the page table.
548 */
549 if (pVM->mm.s.fPGMInitialized)
550 {
551 for (RTGCPHYS offCur = off; offCur < offEnd; offCur += PAGE_SIZE)
552 {
553 RTHCPHYS HCPhys;
554 rc = PGMR3PhysMMIO2GetHCPhys(pVM, pDevIns, iRegion, offCur, &HCPhys);
555 AssertRCReturn(rc, VERR_INTERNAL_ERROR);
556 rc = PGMMap(pVM, GCPtr + (offCur - off), HCPhys, PAGE_SIZE, 0);
557 if (RT_FAILURE(rc))
558 {
559 AssertMsgFailed(("rc=%Rrc offCur=%RGp %s\n", rc, offCur, pszDesc));
560 break;
561 }
562 }
563 }
564
565 if (RT_SUCCESS(rc))
566 {
567 GCPtr |= offPage;
568 *pRCPtr = GCPtr;
569 AssertLogRelReturn(*pRCPtr == GCPtr, VERR_INTERNAL_ERROR);
570 }
571 }
572 return rc;
573}
574
575
576/**
577 * Maps locked R3 virtual memory into the hypervisor region in the GC.
578 *
579 * @return VBox status code.
580 *
581 * @param pVM VM handle.
582 * @param pvR3 The ring-3 address of the memory, must be page aligned.
583 * @param pvR0 The ring-0 address of the memory, must be page aligned. (optional)
584 * @param cPages The number of pages.
585 * @param paPages The page descriptors.
586 * @param pszDesc Mapping description.
587 * @param pGCPtr Where to store the GC address corresponding to pvR3.
588 */
589VMMR3DECL(int) MMR3HyperMapPages(PVM pVM, void *pvR3, RTR0PTR pvR0, size_t cPages, PCSUPPAGE paPages, const char *pszDesc, PRTGCPTR pGCPtr)
590{
591 LogFlow(("MMR3HyperMapPages: pvR3=%p pvR0=%p cPages=%zu paPages=%p pszDesc=%p:{%s} pGCPtr=%p\n",
592 pvR3, pvR0, cPages, paPages, pszDesc, pszDesc, pGCPtr));
593
594 /*
595 * Validate input.
596 */
597 AssertPtrReturn(pvR3, VERR_INVALID_POINTER);
598 AssertPtrReturn(paPages, VERR_INVALID_POINTER);
599 AssertReturn(cPages > 0, VERR_PAGE_COUNT_OUT_OF_RANGE);
600 AssertReturn(cPages <= VBOX_MAX_ALLOC_PAGE_COUNT, VERR_PAGE_COUNT_OUT_OF_RANGE);
601 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
602 AssertReturn(*pszDesc, VERR_INVALID_PARAMETER);
603 AssertPtrReturn(pGCPtr, VERR_INVALID_PARAMETER);
604
605 /*
606 * Add the memory to the hypervisor area.
607 */
608 RTGCPTR GCPtr;
609 PMMLOOKUPHYPER pLookup;
610 int rc = mmR3HyperMap(pVM, cPages << PAGE_SHIFT, pszDesc, &GCPtr, &pLookup);
611 if (RT_SUCCESS(rc))
612 {
613 /*
614 * Copy the physical page addresses and tell PGM about them.
615 */
616 PRTHCPHYS paHCPhysPages = (PRTHCPHYS)MMR3HeapAlloc(pVM, MM_TAG_MM, sizeof(RTHCPHYS) * cPages);
617 if (paHCPhysPages)
618 {
619 for (size_t i = 0; i < cPages; i++)
620 {
621 AssertReleaseMsgReturn(paPages[i].Phys != 0 && paPages[i].Phys != NIL_RTHCPHYS && !(paPages[i].Phys & PAGE_OFFSET_MASK),
622 ("i=%#zx Phys=%RHp %s\n", i, paPages[i].Phys, pszDesc),
623 VERR_INTERNAL_ERROR);
624 paHCPhysPages[i] = paPages[i].Phys;
625 }
626
627 if (pVM->mm.s.fPGMInitialized)
628 {
629 for (size_t i = 0; i < cPages; i++)
630 {
631 rc = PGMMap(pVM, GCPtr + (i << PAGE_SHIFT), paHCPhysPages[i], PAGE_SIZE, 0);
632 AssertRCBreak(rc);
633 }
634 }
635 if (RT_SUCCESS(rc))
636 {
637 pLookup->enmType = MMLOOKUPHYPERTYPE_LOCKED;
638 pLookup->u.Locked.pvR3 = pvR3;
639 pLookup->u.Locked.pvR0 = pvR0;
640 pLookup->u.Locked.paHCPhysPages = paHCPhysPages;
641
642 /* done. */
643 *pGCPtr = GCPtr;
644 return rc;
645 }
646 /* Don't care about failure clean, we're screwed if this fails anyway. */
647 }
648 }
649
650 return rc;
651}
652
653
654/**
655 * Reserves a hypervisor memory area.
656 * Most frequent usage is fence pages and dynamically mappings like the guest PD and PDPT.
657 *
658 * @return VBox status code.
659 *
660 * @param pVM VM handle.
661 * @param cb Size of the memory. Will be rounded up to nearest page.
662 * @param pszDesc Mapping description.
663 * @param pGCPtr Where to store the assigned GC address. Optional.
664 */
665VMMR3DECL(int) MMR3HyperReserve(PVM pVM, unsigned cb, const char *pszDesc, PRTGCPTR pGCPtr)
666{
667 LogFlow(("MMR3HyperMapHCRam: cb=%d pszDesc=%p:{%s} pGCPtr=%p\n", (int)cb, pszDesc, pszDesc, pGCPtr));
668
669 /*
670 * Validate input.
671 */
672 if ( cb <= 0
673 || !pszDesc
674 || !*pszDesc)
675 {
676 AssertMsgFailed(("Invalid parameter\n"));
677 return VERR_INVALID_PARAMETER;
678 }
679
680 /*
681 * Add the memory to the hypervisor area.
682 */
683 RTGCPTR GCPtr;
684 PMMLOOKUPHYPER pLookup;
685 int rc = mmR3HyperMap(pVM, cb, pszDesc, &GCPtr, &pLookup);
686 if (RT_SUCCESS(rc))
687 {
688 pLookup->enmType = MMLOOKUPHYPERTYPE_DYNAMIC;
689 if (pGCPtr)
690 *pGCPtr = GCPtr;
691 return VINF_SUCCESS;
692 }
693 return rc;
694}
695
696
697/**
698 * Adds memory to the hypervisor memory arena.
699 *
700 * @return VBox status code.
701 * @param pVM The VM handle.
702 * @param cb Size of the memory. Will be rounded up to neares page.
703 * @param pszDesc The description of the memory.
704 * @param pGCPtr Where to store the GC address.
705 * @param ppLookup Where to store the pointer to the lookup record.
706 * @remark We assume the threading structure of VBox imposes natural
707 * serialization of most functions, this one included.
708 */
709static int mmR3HyperMap(PVM pVM, const size_t cb, const char *pszDesc, PRTGCPTR pGCPtr, PMMLOOKUPHYPER *ppLookup)
710{
711 /*
712 * Validate input.
713 */
714 const uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
715 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
716 if (pVM->mm.s.offHyperNextStatic + cbAligned >= pVM->mm.s.cbHyperArea) /* don't use the last page, it's a fence. */
717 {
718 AssertMsgFailed(("Out of static mapping space in the HMA! offHyperAreaGC=%x cbAligned=%x cbHyperArea=%x\n",
719 pVM->mm.s.offHyperNextStatic, cbAligned, pVM->mm.s.cbHyperArea));
720 return VERR_NO_MEMORY;
721 }
722
723 /*
724 * Allocate lookup record.
725 */
726 PMMLOOKUPHYPER pLookup;
727 int rc = MMHyperAlloc(pVM, sizeof(*pLookup), 1, MM_TAG_MM, (void **)&pLookup);
728 if (RT_SUCCESS(rc))
729 {
730 /*
731 * Initialize it and insert it.
732 */
733 pLookup->offNext = pVM->mm.s.offLookupHyper;
734 pLookup->cb = cbAligned;
735 pLookup->off = pVM->mm.s.offHyperNextStatic;
736 pVM->mm.s.offLookupHyper = (uint8_t *)pLookup - (uint8_t *)pVM->mm.s.pHyperHeapR3;
737 if (pLookup->offNext != (int32_t)NIL_OFFSET)
738 pLookup->offNext -= pVM->mm.s.offLookupHyper;
739 pLookup->enmType = MMLOOKUPHYPERTYPE_INVALID;
740 memset(&pLookup->u, 0xff, sizeof(pLookup->u));
741 pLookup->pszDesc = pszDesc;
742
743 /* Mapping. */
744 *pGCPtr = pVM->mm.s.pvHyperAreaGC + pVM->mm.s.offHyperNextStatic;
745 pVM->mm.s.offHyperNextStatic += cbAligned;
746
747 /* Return pointer. */
748 *ppLookup = pLookup;
749 }
750
751 AssertRC(rc);
752 LogFlow(("mmR3HyperMap: returns %Rrc *pGCPtr=%RGv\n", rc, *pGCPtr));
753 return rc;
754}
755
756
757/**
758 * Allocates a new heap.
759 *
760 * @returns VBox status code.
761 * @param pVM The VM handle.
762 * @param cb The size of the new heap.
763 * @param ppHeap Where to store the heap pointer on successful return.
764 * @param pR0PtrHeap Where to store the ring-0 address of the heap on
765 * success.
766 */
767static int mmR3HyperHeapCreate(PVM pVM, const size_t cb, PMMHYPERHEAP *ppHeap, PRTR0PTR pR0PtrHeap)
768{
769 /*
770 * Allocate the hypervisor heap.
771 */
772 const uint32_t cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
773 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
774 uint32_t const cPages = cbAligned >> PAGE_SHIFT;
775 PSUPPAGE paPages = (PSUPPAGE)MMR3HeapAlloc(pVM, MM_TAG_MM, cPages * sizeof(paPages[0]));
776 if (!paPages)
777 return VERR_NO_MEMORY;
778 void *pv;
779 RTR0PTR pvR0 = NIL_RTR0PTR;
780 int rc = SUPR3PageAllocEx(cPages,
781 0 /*fFlags*/,
782 &pv,
783#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
784 VMMIsHwVirtExtForced(pVM) ? &pvR0 : NULL,
785#else
786 NULL,
787#endif
788 paPages);
789 if (RT_SUCCESS(rc))
790 {
791#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
792 if (!VMMIsHwVirtExtForced(pVM))
793 pvR0 = NIL_RTR0PTR;
794#else
795 pvR0 = (uintptr_t)pv;
796#endif
797 memset(pv, 0, cbAligned);
798
799 /*
800 * Initialize the heap and first free chunk.
801 */
802 PMMHYPERHEAP pHeap = (PMMHYPERHEAP)pv;
803 pHeap->u32Magic = MMHYPERHEAP_MAGIC;
804 pHeap->pbHeapR3 = (uint8_t *)pHeap + MMYPERHEAP_HDR_SIZE;
805 pHeap->pbHeapR0 = pvR0 != NIL_RTR0PTR ? pvR0 + MMYPERHEAP_HDR_SIZE : NIL_RTR0PTR;
806 //pHeap->pbHeapRC = 0; // set by mmR3HyperHeapMap()
807 pHeap->pVMR3 = pVM;
808 pHeap->pVMR0 = pVM->pVMR0;
809 pHeap->pVMRC = pVM->pVMRC;
810 pHeap->cbHeap = cbAligned - MMYPERHEAP_HDR_SIZE;
811 pHeap->cbFree = pHeap->cbHeap - sizeof(MMHYPERCHUNK);
812 //pHeap->offFreeHead = 0;
813 //pHeap->offFreeTail = 0;
814 pHeap->offPageAligned = pHeap->cbHeap;
815 //pHeap->HyperHeapStatTree = 0;
816 pHeap->paPages = paPages;
817
818 PMMHYPERCHUNKFREE pFree = (PMMHYPERCHUNKFREE)pHeap->pbHeapR3;
819 pFree->cb = pHeap->cbFree;
820 //pFree->core.offNext = 0;
821 MMHYPERCHUNK_SET_TYPE(&pFree->core, MMHYPERCHUNK_FLAGS_FREE);
822 pFree->core.offHeap = -(int32_t)MMYPERHEAP_HDR_SIZE;
823 //pFree->offNext = 0;
824 //pFree->offPrev = 0;
825
826 STAMR3Register(pVM, &pHeap->cbHeap, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, "/MM/HyperHeap/cbHeap", STAMUNIT_BYTES, "The heap size.");
827 STAMR3Register(pVM, &pHeap->cbFree, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, "/MM/HyperHeap/cbFree", STAMUNIT_BYTES, "The free space.");
828
829 *ppHeap = pHeap;
830 *pR0PtrHeap = pvR0;
831 return VINF_SUCCESS;
832 }
833 AssertMsgFailed(("SUPR3PageAllocEx(%d,,,,) -> %Rrc\n", cbAligned >> PAGE_SHIFT, rc));
834
835 *ppHeap = NULL;
836 return rc;
837}
838
839/**
840 * Allocates a new heap.
841 */
842static int mmR3HyperHeapMap(PVM pVM, PMMHYPERHEAP pHeap, PRTGCPTR ppHeapGC)
843{
844 Assert(RT_ALIGN_Z(pHeap->cbHeap + MMYPERHEAP_HDR_SIZE, PAGE_SIZE) == pHeap->cbHeap + MMYPERHEAP_HDR_SIZE);
845 Assert(pHeap->paPages);
846 int rc = MMR3HyperMapPages(pVM,
847 pHeap,
848 pHeap->pbHeapR0 != NIL_RTR0PTR ? pHeap->pbHeapR0 - MMYPERHEAP_HDR_SIZE : NIL_RTR0PTR,
849 (pHeap->cbHeap + MMYPERHEAP_HDR_SIZE) >> PAGE_SHIFT,
850 pHeap->paPages,
851 "Heap", ppHeapGC);
852 if (RT_SUCCESS(rc))
853 {
854 pHeap->pVMRC = pVM->pVMRC;
855 pHeap->pbHeapRC = *ppHeapGC + MMYPERHEAP_HDR_SIZE;
856 /* Reserve a page for fencing. */
857 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
858
859 /* We won't need these any more. */
860 MMR3HeapFree(pHeap->paPages);
861 pHeap->paPages = NULL;
862 }
863 return rc;
864}
865
866
867/**
868 * Allocates memory in the Hypervisor (GC VMM) area which never will
869 * be freed and doesn't have any offset based relation to other heap blocks.
870 *
871 * The latter means that two blocks allocated by this API will not have the
872 * same relative position to each other in GC and HC. In short, never use
873 * this API for allocating nodes for an offset based AVL tree!
874 *
875 * The returned memory is of course zeroed.
876 *
877 * @returns VBox status code.
878 * @param pVM The VM to operate on.
879 * @param cb Number of bytes to allocate.
880 * @param uAlignment Required memory alignment in bytes.
881 * Values are 0,8,16,32 and PAGE_SIZE.
882 * 0 -> default alignment, i.e. 8 bytes.
883 * @param enmTag The statistics tag.
884 * @param ppv Where to store the address to the allocated
885 * memory.
886 * @remark This is assumed not to be used at times when serialization is required.
887 */
888VMMR3DECL(int) MMR3HyperAllocOnceNoRel(PVM pVM, size_t cb, unsigned uAlignment, MMTAG enmTag, void **ppv)
889{
890 return MMR3HyperAllocOnceNoRelEx(pVM, cb, uAlignment, enmTag, 0/*fFlags*/, ppv);
891}
892
893
894/**
895 * Allocates memory in the Hypervisor (GC VMM) area which never will
896 * be freed and doesn't have any offset based relation to other heap blocks.
897 *
898 * The latter means that two blocks allocated by this API will not have the
899 * same relative position to each other in GC and HC. In short, never use
900 * this API for allocating nodes for an offset based AVL tree!
901 *
902 * The returned memory is of course zeroed.
903 *
904 * @returns VBox status code.
905 * @param pVM The VM to operate on.
906 * @param cb Number of bytes to allocate.
907 * @param uAlignment Required memory alignment in bytes.
908 * Values are 0,8,16,32 and PAGE_SIZE.
909 * 0 -> default alignment, i.e. 8 bytes.
910 * @param enmTag The statistics tag.
911 * @param fFlags Flags, see MMHYPER_AONR_FLAGS_KERNEL_MAPPING.
912 * @param ppv Where to store the address to the allocated memory.
913 * @remark This is assumed not to be used at times when serialization is required.
914 */
915VMMR3DECL(int) MMR3HyperAllocOnceNoRelEx(PVM pVM, size_t cb, unsigned uAlignment, MMTAG enmTag, uint32_t fFlags, void **ppv)
916{
917 AssertMsg(cb >= 8, ("Hey! Do you really mean to allocate less than 8 bytes?! cb=%d\n", cb));
918 Assert(!(fFlags & ~(MMHYPER_AONR_FLAGS_KERNEL_MAPPING)));
919
920 /*
921 * Choose between allocating a new chunk of HMA memory
922 * and the heap. We will only do BIG allocations from HMA and
923 * only at creation time.
924 */
925 if ( ( cb < _64K
926 && ( uAlignment != PAGE_SIZE
927 || cb < 48*_1K)
928 && !(fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING)
929 )
930 || VMR3GetState(pVM) != VMSTATE_CREATING
931 )
932 {
933 Assert(!(fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING));
934 int rc = MMHyperAlloc(pVM, cb, uAlignment, enmTag, ppv);
935 if ( rc != VERR_MM_HYPER_NO_MEMORY
936 || cb <= 8*_1K)
937 {
938 Log2(("MMR3HyperAllocOnceNoRel: cb=%#zx uAlignment=%#x returns %Rrc and *ppv=%p\n",
939 cb, uAlignment, rc, *ppv));
940 return rc;
941 }
942 }
943
944#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
945 /*
946 * Set MMHYPER_AONR_FLAGS_KERNEL_MAPPING if we're in going to execute in ring-0.
947 */
948 if (VMMIsHwVirtExtForced(pVM))
949 fFlags |= MMHYPER_AONR_FLAGS_KERNEL_MAPPING;
950#endif
951
952 /*
953 * Validate alignment.
954 */
955 switch (uAlignment)
956 {
957 case 0:
958 case 8:
959 case 16:
960 case 32:
961 case PAGE_SIZE:
962 break;
963 default:
964 AssertMsgFailed(("Invalid alignment %u\n", uAlignment));
965 return VERR_INVALID_PARAMETER;
966 }
967
968 /*
969 * Allocate the pages and map them into HMA space.
970 */
971 uint32_t const cbAligned = RT_ALIGN_32(cb, PAGE_SIZE);
972 AssertReturn(cbAligned >= cb, VERR_INVALID_PARAMETER);
973 uint32_t const cPages = cbAligned >> PAGE_SHIFT;
974 PSUPPAGE paPages = (PSUPPAGE)RTMemTmpAlloc(cPages * sizeof(paPages[0]));
975 if (!paPages)
976 return VERR_NO_TMP_MEMORY;
977 void *pvPages;
978 RTR0PTR pvR0 = NIL_RTR0PTR;
979 int rc = SUPR3PageAllocEx(cPages,
980 0 /*fFlags*/,
981 &pvPages,
982 fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING ? &pvR0 : NULL,
983 paPages);
984 if (RT_SUCCESS(rc))
985 {
986 if (!(fFlags & MMHYPER_AONR_FLAGS_KERNEL_MAPPING))
987#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
988 pvR0 = NIL_RTR0PTR;
989#else
990 pvR0 = (RTR0PTR)pvPages;
991#endif
992
993 memset(pvPages, 0, cbAligned);
994
995 RTGCPTR GCPtr;
996 rc = MMR3HyperMapPages(pVM,
997 pvPages,
998 pvR0,
999 cPages,
1000 paPages,
1001 MMR3HeapAPrintf(pVM, MM_TAG_MM, "alloc once (%s)", mmGetTagName(enmTag)),
1002 &GCPtr);
1003 if (RT_SUCCESS(rc))
1004 {
1005 *ppv = pvPages;
1006 Log2(("MMR3HyperAllocOnceNoRel: cbAligned=%#x uAlignment=%#x returns VINF_SUCCESS and *ppv=%p\n",
1007 cbAligned, uAlignment, *ppv));
1008 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
1009 return rc;
1010 }
1011 AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
1012 SUPR3PageFreeEx(pvPages, cPages);
1013
1014
1015 /*
1016 * HACK ALERT! Try allocate it off the heap so that we don't freak
1017 * out during vga/vmmdev mmio2 allocation with certain ram sizes.
1018 */
1019 /** @todo make a proper fix for this so we will never end up in this kind of situation! */
1020 Log(("MMR3HyperAllocOnceNoRel: MMR3HyperMapHCRam failed with rc=%Rrc, try MMHyperAlloc(,%#x,,) instead\n", rc, cb));
1021 int rc2 = MMHyperAlloc(pVM, cb, uAlignment, enmTag, ppv);
1022 if (RT_SUCCESS(rc2))
1023 {
1024 Log2(("MMR3HyperAllocOnceNoRel: cb=%#x uAlignment=%#x returns %Rrc and *ppv=%p\n",
1025 cb, uAlignment, rc, *ppv));
1026 return rc;
1027 }
1028 }
1029 else
1030 AssertMsgFailed(("Failed to allocate %zd bytes! %Rrc\n", cbAligned, rc));
1031
1032 if (rc == VERR_NO_MEMORY)
1033 rc = VERR_MM_HYPER_NO_MEMORY;
1034 LogRel(("MMR3HyperAllocOnceNoRel: cb=%#zx uAlignment=%#x returns %Rrc\n", cb, uAlignment, rc));
1035 return rc;
1036}
1037
1038
1039/**
1040 * Lookus up a ring-3 pointer to HMA.
1041 *
1042 * @returns The lookup record on success, NULL on failure.
1043 * @param pVM The VM handle.
1044 * @param pvR3 The ring-3 address to look up.
1045 */
1046DECLINLINE(PMMLOOKUPHYPER) mmR3HyperLookupR3(PVM pVM, void *pvR3)
1047{
1048 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1049 for (;;)
1050 {
1051 switch (pLookup->enmType)
1052 {
1053 case MMLOOKUPHYPERTYPE_LOCKED:
1054 {
1055 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.Locked.pvR3;
1056 if (off < pLookup->cb)
1057 return pLookup;
1058 break;
1059 }
1060
1061 case MMLOOKUPHYPERTYPE_HCPHYS:
1062 {
1063 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.HCPhys.pvR3;
1064 if (off < pLookup->cb)
1065 return pLookup;
1066 break;
1067 }
1068
1069 case MMLOOKUPHYPERTYPE_GCPHYS:
1070 case MMLOOKUPHYPERTYPE_MMIO2:
1071 case MMLOOKUPHYPERTYPE_DYNAMIC:
1072 /** @todo ? */
1073 break;
1074
1075 default:
1076 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1077 return NULL;
1078 }
1079
1080 /* next */
1081 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1082 return NULL;
1083 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1084 }
1085}
1086
1087
1088/**
1089 * Set / unset guard status on one or more hyper heap pages.
1090 *
1091 * @returns VBox status code (first failure).
1092 * @param pVM The VM handle.
1093 * @param pvStart The hyper heap page address. Must be page
1094 * aligned.
1095 * @param cb The number of bytes. Must be page aligned.
1096 * @param fSet Wheter to set or unset guard page status.
1097 */
1098VMMR3DECL(int) MMR3HyperSetGuard(PVM pVM, void *pvStart, size_t cb, bool fSet)
1099{
1100 /*
1101 * Validate input.
1102 */
1103 AssertReturn(!((uintptr_t)pvStart & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1104 AssertReturn(!(cb & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
1105 AssertReturn(cb <= UINT32_MAX, VERR_INVALID_PARAMETER);
1106 PMMLOOKUPHYPER pLookup = mmR3HyperLookupR3(pVM, pvStart);
1107 AssertReturn(pLookup, VERR_INVALID_PARAMETER);
1108 AssertReturn(pLookup->enmType == MMLOOKUPHYPERTYPE_LOCKED, VERR_INVALID_PARAMETER);
1109
1110 /*
1111 * Get down to business.
1112 * Note! We quietly ignore errors from the support library since the
1113 * protection stuff isn't possible to implement on all platforms.
1114 */
1115 uint8_t *pbR3 = (uint8_t *)pLookup->u.Locked.pvR3;
1116 RTR0PTR R0Ptr = pLookup->u.Locked.pvR0 != (uintptr_t)pLookup->u.Locked.pvR3
1117 ? pLookup->u.Locked.pvR0
1118 : NIL_RTR0PTR;
1119 uint32_t off = (uint32_t)((uint8_t *)pvStart - pbR3);
1120 int rc;
1121 if (fSet)
1122 {
1123 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, 0);
1124 SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_NONE);
1125 }
1126 else
1127 {
1128 rc = PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pvStart), cb, X86_PTE_P | X86_PTE_A | X86_PTE_D | X86_PTE_RW);
1129 SUPR3PageProtect(pbR3, R0Ptr, off, (uint32_t)cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
1130 }
1131 return rc;
1132}
1133
1134
1135/**
1136 * Convert hypervisor HC virtual address to HC physical address.
1137 *
1138 * @returns HC physical address.
1139 * @param pVM VM Handle
1140 * @param pvR3 Host context virtual address.
1141 */
1142VMMR3DECL(RTHCPHYS) MMR3HyperHCVirt2HCPhys(PVM pVM, void *pvR3)
1143{
1144 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1145 for (;;)
1146 {
1147 switch (pLookup->enmType)
1148 {
1149 case MMLOOKUPHYPERTYPE_LOCKED:
1150 {
1151 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.Locked.pvR3;
1152 if (off < pLookup->cb)
1153 return pLookup->u.Locked.paHCPhysPages[off >> PAGE_SHIFT] | (off & PAGE_OFFSET_MASK);
1154 break;
1155 }
1156
1157 case MMLOOKUPHYPERTYPE_HCPHYS:
1158 {
1159 unsigned off = (uint8_t *)pvR3 - (uint8_t *)pLookup->u.HCPhys.pvR3;
1160 if (off < pLookup->cb)
1161 return pLookup->u.HCPhys.HCPhys + off;
1162 break;
1163 }
1164
1165 case MMLOOKUPHYPERTYPE_GCPHYS:
1166 case MMLOOKUPHYPERTYPE_MMIO2:
1167 case MMLOOKUPHYPERTYPE_DYNAMIC:
1168 /* can (or don't want to) convert these kind of records. */
1169 break;
1170
1171 default:
1172 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1173 break;
1174 }
1175
1176 /* next */
1177 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1178 break;
1179 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1180 }
1181
1182 AssertMsgFailed(("pvR3=%p is not inside the hypervisor memory area!\n", pvR3));
1183 return NIL_RTHCPHYS;
1184}
1185
1186
1187/**
1188 * Implements the return case of MMR3HyperQueryInfoFromHCPhys.
1189 *
1190 * @returns VINF_SUCCESS, VINF_BUFFER_OVERFLOW.
1191 * @param pVM The VM handle.
1192 * @param HCPhys The host physical address to look for.
1193 * @param pLookup The HMA lookup entry corresponding to HCPhys.
1194 * @param pszWhat Where to return the description.
1195 * @param cbWhat Size of the return buffer.
1196 * @param pcbAlloc Where to return the size of whatever it is.
1197 */
1198static int mmR3HyperQueryInfoFromHCPhysFound(PVM pVM, RTHCPHYS HCPhys, PMMLOOKUPHYPER pLookup,
1199 char *pszWhat, size_t cbWhat, uint32_t *pcbAlloc)
1200{
1201 *pcbAlloc = pLookup->cb;
1202 int rc = RTStrCopy(pszWhat, cbWhat, pLookup->pszDesc);
1203 return rc == VERR_BUFFER_OVERFLOW ? VINF_BUFFER_OVERFLOW : rc;
1204}
1205
1206
1207/**
1208 * Scans the HMA for the physical page and reports back a description if found.
1209 *
1210 * @returns VINF_SUCCESS, VINF_BUFFER_OVERFLOW, VERR_NOT_FOUND.
1211 * @param pVM The VM handle.
1212 * @param HCPhys The host physical address to look for.
1213 * @param pszWhat Where to return the description.
1214 * @param cbWhat Size of the return buffer.
1215 * @param pcbAlloc Where to return the size of whatever it is.
1216 */
1217VMMR3_INT_DECL(int) MMR3HyperQueryInfoFromHCPhys(PVM pVM, RTHCPHYS HCPhys, char *pszWhat, size_t cbWhat, uint32_t *pcbAlloc)
1218{
1219 RTHCPHYS HCPhysPage = HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK;
1220 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1221 for (;;)
1222 {
1223 switch (pLookup->enmType)
1224 {
1225 case MMLOOKUPHYPERTYPE_LOCKED:
1226 {
1227 uint32_t i = pLookup->cb >> PAGE_SHIFT;
1228 while (i-- > 0)
1229 if (pLookup->u.Locked.paHCPhysPages[i] == HCPhysPage)
1230 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1231 break;
1232 }
1233
1234 case MMLOOKUPHYPERTYPE_HCPHYS:
1235 {
1236 if (pLookup->u.HCPhys.HCPhys - HCPhysPage < pLookup->cb)
1237 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1238 break;
1239 }
1240
1241 case MMLOOKUPHYPERTYPE_MMIO2:
1242 case MMLOOKUPHYPERTYPE_GCPHYS:
1243 case MMLOOKUPHYPERTYPE_DYNAMIC:
1244 {
1245 /* brute force. */
1246 uint32_t i = pLookup->cb >> PAGE_SHIFT;
1247 while (i-- > 0)
1248 {
1249 RTGCPTR GCPtr = pLookup->off + pVM->mm.s.pvHyperAreaGC;
1250 RTHCPHYS HCPhysCur;
1251 int rc = PGMMapGetPage(pVM, GCPtr, NULL, &HCPhysCur);
1252 if (RT_SUCCESS(rc) && HCPhysCur == HCPhysPage)
1253 return mmR3HyperQueryInfoFromHCPhysFound(pVM, HCPhys, pLookup, pszWhat, cbWhat, pcbAlloc);
1254 }
1255 break;
1256 }
1257 default:
1258 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1259 break;
1260 }
1261
1262 /* next */
1263 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1264 break;
1265 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1266 }
1267 return VERR_NOT_FOUND;
1268}
1269
1270
1271#if 0 /* unused, not implemented */
1272/**
1273 * Convert hypervisor HC physical address to HC virtual address.
1274 *
1275 * @returns HC virtual address.
1276 * @param pVM VM Handle
1277 * @param HCPhys Host context physical address.
1278 */
1279VMMR3DECL(void *) MMR3HyperHCPhys2HCVirt(PVM pVM, RTHCPHYS HCPhys)
1280{
1281 void *pv;
1282 int rc = MMR3HyperHCPhys2HCVirtEx(pVM, HCPhys, &pv);
1283 if (RT_SUCCESS(rc))
1284 return pv;
1285 AssertMsgFailed(("Invalid address HCPhys=%x rc=%d\n", HCPhys, rc));
1286 return NULL;
1287}
1288
1289
1290/**
1291 * Convert hypervisor HC physical address to HC virtual address.
1292 *
1293 * @returns VBox status.
1294 * @param pVM VM Handle
1295 * @param HCPhys Host context physical address.
1296 * @param ppv Where to store the HC virtual address.
1297 */
1298VMMR3DECL(int) MMR3HyperHCPhys2HCVirtEx(PVM pVM, RTHCPHYS HCPhys, void **ppv)
1299{
1300 /*
1301 * Linear search.
1302 */
1303 /** @todo implement when actually used. */
1304 return VERR_INVALID_POINTER;
1305}
1306#endif /* unused, not implemented */
1307
1308
1309/**
1310 * Read hypervisor memory from GC virtual address.
1311 *
1312 * @returns VBox status.
1313 * @param pVM VM handle.
1314 * @param pvDst Destination address (HC of course).
1315 * @param GCPtr GC virtual address.
1316 * @param cb Number of bytes to read.
1317 *
1318 * @remarks For DBGF only.
1319 */
1320VMMR3DECL(int) MMR3HyperReadGCVirt(PVM pVM, void *pvDst, RTGCPTR GCPtr, size_t cb)
1321{
1322 if (GCPtr - pVM->mm.s.pvHyperAreaGC >= pVM->mm.s.cbHyperArea)
1323 return VERR_INVALID_POINTER;
1324 return PGMR3MapRead(pVM, pvDst, GCPtr, cb);
1325}
1326
1327
1328/**
1329 * Info handler for 'hma', it dumps the list of lookup records for the hypervisor memory area.
1330 *
1331 * @param pVM The VM handle.
1332 * @param pHlp Callback functions for doing output.
1333 * @param pszArgs Argument string. Optional and specific to the handler.
1334 */
1335static DECLCALLBACK(void) mmR3HyperInfoHma(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1336{
1337 pHlp->pfnPrintf(pHlp, "Hypervisor Memory Area (HMA) Layout: Base %RGv, 0x%08x bytes\n",
1338 pVM->mm.s.pvHyperAreaGC, pVM->mm.s.cbHyperArea);
1339
1340 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.pHyperHeapR3 + pVM->mm.s.offLookupHyper);
1341 for (;;)
1342 {
1343 switch (pLookup->enmType)
1344 {
1345 case MMLOOKUPHYPERTYPE_LOCKED:
1346 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv LOCKED %-*s %s\n",
1347 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1348 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1349 pLookup->u.Locked.pvR3,
1350 pLookup->u.Locked.pvR0,
1351 sizeof(RTHCPTR) * 2, "",
1352 pLookup->pszDesc);
1353 break;
1354
1355 case MMLOOKUPHYPERTYPE_HCPHYS:
1356 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %RHv %RHv HCPHYS %RHp %s\n",
1357 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1358 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1359 pLookup->u.HCPhys.pvR3,
1360 pLookup->u.HCPhys.pvR0,
1361 pLookup->u.HCPhys.HCPhys,
1362 pLookup->pszDesc);
1363 break;
1364
1365 case MMLOOKUPHYPERTYPE_GCPHYS:
1366 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s GCPHYS %RGp%*s %s\n",
1367 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1368 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1369 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1370 pLookup->u.GCPhys.GCPhys, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
1371 pLookup->pszDesc);
1372 break;
1373
1374 case MMLOOKUPHYPERTYPE_MMIO2:
1375 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s MMIO2 %RGp%*s %s\n",
1376 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1377 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1378 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1379 pLookup->u.MMIO2.off, RT_ABS((int)(sizeof(RTHCPHYS) - sizeof(RTGCPHYS))) * 2, "",
1380 pLookup->pszDesc);
1381 break;
1382
1383 case MMLOOKUPHYPERTYPE_DYNAMIC:
1384 pHlp->pfnPrintf(pHlp, "%RGv-%RGv %*s DYNAMIC %*s %s\n",
1385 pLookup->off + pVM->mm.s.pvHyperAreaGC,
1386 pLookup->off + pVM->mm.s.pvHyperAreaGC + pLookup->cb,
1387 sizeof(RTHCPTR) * 2 * 2 + 1, "",
1388 sizeof(RTHCPTR) * 2, "",
1389 pLookup->pszDesc);
1390 break;
1391
1392 default:
1393 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
1394 break;
1395 }
1396
1397 /* next */
1398 if ((unsigned)pLookup->offNext == NIL_OFFSET)
1399 break;
1400 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
1401 }
1402}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette