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