VirtualBox

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

最後變更 在這個檔案從32908是 32908,由 vboxsync 提交於 14 年 前

VMM: moved hyper heap configuration to console, as it knows more about VM config, and logic getting complicated

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette