VirtualBox

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

Last change on this file since 30690 was 30493, checked in by vboxsync, 14 years ago

Demoted some PGM apis to internal only.

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