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