VirtualBox

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

Last change on this file since 45480 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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