VirtualBox

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

Last change on this file since 64115 was 62478, checked in by vboxsync, 8 years ago

(C) 2016

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