VirtualBox

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

Last change on this file since 47682 was 45618, checked in by vboxsync, 12 years ago

Do HMR3Init first in vmR3InitRing3 so the other components can skip raw-mode bits during init.

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