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