VirtualBox

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

Last change on this file since 29655 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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