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