VirtualBox

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

Last change on this file since 80239 was 80191, checked in by vboxsync, 5 years ago

VMM/r3: Refactored VMCPU enumeration in preparation that aCpus will be replaced with a pointer array. Removed two raw-mode offset members from the CPUM and CPUMCPU sub-structures. bugref:9217 bugref:9517

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