VirtualBox

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

Last change on this file since 50112 was 50112, checked in by vboxsync, 10 years ago

Missed one line.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use