1 | /* $Id: MMPhys.cpp 6535 2008-01-28 19:16:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MM - Memory Monitor(/Manager) - Physical Memory.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_MM_PHYS
|
---|
23 | #include <VBox/mm.h>
|
---|
24 | #include <VBox/pgm.h>
|
---|
25 | #include <VBox/rem.h>
|
---|
26 | #include "MMInternal.h"
|
---|
27 | #include <VBox/vm.h>
|
---|
28 |
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #include <VBox/param.h>
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <iprt/alloc.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Register externally allocated RAM for the virtual machine.
|
---|
39 | *
|
---|
40 | * The memory registered with the VM thru this interface must not be freed
|
---|
41 | * before the virtual machine has been destroyed. Bad things may happen... :-)
|
---|
42 | *
|
---|
43 | * @return VBox status code.
|
---|
44 | * @param pVM VM handle.
|
---|
45 | * @param pvRam Virtual address of the guest's physical memory range Must be page aligned.
|
---|
46 | * @param GCPhys The physical address the ram shall be registered at.
|
---|
47 | * @param cb Size of the memory. Must be page aligend.
|
---|
48 | * @param fFlags Flags of the MM_RAM_FLAGS_* defines.
|
---|
49 | * @param pszDesc Description of the memory.
|
---|
50 | */
|
---|
51 | MMR3DECL(int) MMR3PhysRegister(PVM pVM, void *pvRam, RTGCPHYS GCPhys, unsigned cb, unsigned fFlags, const char *pszDesc)
|
---|
52 | {
|
---|
53 | return MMR3PhysRegisterEx(pVM, pvRam, GCPhys, cb, fFlags, MM_PHYS_TYPE_NORMAL, pszDesc);
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Register externally allocated RAM for the virtual machine.
|
---|
59 | *
|
---|
60 | * The memory registered with the VM thru this interface must not be freed
|
---|
61 | * before the virtual machine has been destroyed. Bad things may happen... :-)
|
---|
62 | *
|
---|
63 | * @return VBox status code.
|
---|
64 | * @param pVM VM handle.
|
---|
65 | * @param pvRam Virtual address of the guest's physical memory range Must be page aligned.
|
---|
66 | * @param GCPhys The physical address the ram shall be registered at.
|
---|
67 | * @param cb Size of the memory. Must be page aligend.
|
---|
68 | * @param fFlags Flags of the MM_RAM_FLAGS_* defines.
|
---|
69 | * @param enmType Physical range type (MM_PHYS_TYPE_*)
|
---|
70 | * @param pszDesc Description of the memory.
|
---|
71 | * @thread The Emulation Thread.
|
---|
72 | */
|
---|
73 | /** @todo this function description is not longer up-to-date */
|
---|
74 | MMR3DECL(int) MMR3PhysRegisterEx(PVM pVM, void *pvRam, RTGCPHYS GCPhys, unsigned cb, unsigned fFlags, MMPHYSREG enmType, const char *pszDesc)
|
---|
75 | {
|
---|
76 | int rc = VINF_SUCCESS;
|
---|
77 |
|
---|
78 | Log(("MMR3PhysRegister: pvRam=%p GCPhys=%VGp cb=%#x fFlags=%#x\n", pvRam, GCPhys, cb, fFlags));
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Validate input.
|
---|
82 | */
|
---|
83 | AssertMsg(pVM, ("Invalid VM pointer\n"));
|
---|
84 | if (pvRam)
|
---|
85 | AssertReturn(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam, VERR_INVALID_PARAMETER);
|
---|
86 | else
|
---|
87 | AssertReturn(fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC, VERR_INVALID_PARAMETER);
|
---|
88 | AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
|
---|
89 | AssertReturn(RT_ALIGN_Z(cb, PAGE_SIZE) == cb, VERR_INVALID_PARAMETER);
|
---|
90 | AssertReturn(enmType == MM_PHYS_TYPE_NORMAL || enmType == MM_PHYS_TYPE_DYNALLOC_CHUNK, VERR_INVALID_PARAMETER);
|
---|
91 | RTGCPHYS GCPhysLast = GCPhys + (cb - 1);
|
---|
92 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
93 |
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Check for conflicts.
|
---|
97 | *
|
---|
98 | * We do not support overlapping physical memory regions yet,
|
---|
99 | * even if that's what the MM_RAM_FLAGS_MMIO2 flags is trying to
|
---|
100 | * tell us to do. Provided that all MMIO2 addresses are very high
|
---|
101 | * there is no real danger we'll be able to assign so much memory
|
---|
102 | * for a guest that it'll ever be a problem.
|
---|
103 | */
|
---|
104 | AssertMsg(!(fFlags & MM_RAM_FLAGS_MMIO2) || GCPhys > 0xc0000000,
|
---|
105 | ("MMIO2 addresses should be above 3GB for avoiding conflicts with real RAM.\n"));
|
---|
106 | PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem;
|
---|
107 | while (pCur)
|
---|
108 | {
|
---|
109 | if ( pCur->eType == MM_LOCKED_TYPE_PHYS
|
---|
110 | && ( GCPhys - pCur->u.phys.GCPhys < pCur->cb
|
---|
111 | || pCur->u.phys.GCPhys - GCPhys < cb)
|
---|
112 | )
|
---|
113 | {
|
---|
114 | AssertMsgFailed(("Conflicting RAM range. Existing %#x LB%#x, Req %#x LB%#x\n",
|
---|
115 | pCur->u.phys.GCPhys, pCur->cb, GCPhys, cb));
|
---|
116 | return VERR_MM_RAM_CONFLICT;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* next */
|
---|
120 | pCur = pCur->pNext;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /* Dynamic/on-demand allocation of backing memory? */
|
---|
125 | if (fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
|
---|
126 | {
|
---|
127 | /*
|
---|
128 | * Register the ram with PGM.
|
---|
129 | */
|
---|
130 | rc = PGMR3PhysRegister(pVM, pvRam, GCPhys, cb, fFlags, NULL, pszDesc);
|
---|
131 | if (VBOX_SUCCESS(rc))
|
---|
132 | {
|
---|
133 | if (fFlags == MM_RAM_FLAGS_DYNAMIC_ALLOC)
|
---|
134 | pVM->mm.s.cbRamRegistered += cb;
|
---|
135 |
|
---|
136 | REMR3NotifyPhysRamRegister(pVM, GCPhys, cb, pvRam, fFlags);
|
---|
137 | return rc;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | /*
|
---|
143 | * Lock the memory. (fully allocated by caller)
|
---|
144 | */
|
---|
145 | PMMLOCKEDMEM pLockedMem;
|
---|
146 | rc = mmR3LockMem(pVM, pvRam, cb, MM_LOCKED_TYPE_PHYS, &pLockedMem, enmType == MM_PHYS_TYPE_DYNALLOC_CHUNK /* fSilentFailure */);
|
---|
147 | if (VBOX_SUCCESS(rc))
|
---|
148 | {
|
---|
149 | pLockedMem->u.phys.GCPhys = GCPhys;
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * We set any page flags specified.
|
---|
153 | */
|
---|
154 | if (fFlags)
|
---|
155 | for (unsigned i = 0; i < cb >> PAGE_SHIFT; i++)
|
---|
156 | pLockedMem->aPhysPages[i].Phys |= fFlags;
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Register the ram with PGM.
|
---|
160 | */
|
---|
161 | if (enmType == MM_PHYS_TYPE_NORMAL)
|
---|
162 | {
|
---|
163 | rc = PGMR3PhysRegister(pVM, pvRam, pLockedMem->u.phys.GCPhys, cb, fFlags, &pLockedMem->aPhysPages[0], pszDesc);
|
---|
164 | if (VBOX_SUCCESS(rc))
|
---|
165 | {
|
---|
166 | if (!fFlags)
|
---|
167 | pVM->mm.s.cbRamRegistered += cb;
|
---|
168 |
|
---|
169 | REMR3NotifyPhysRamRegister(pVM, GCPhys, cb, pvRam, fFlags);
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | else
|
---|
174 | {
|
---|
175 | Assert(enmType == MM_PHYS_TYPE_DYNALLOC_CHUNK);
|
---|
176 | return PGMR3PhysRegisterChunk(pVM, pvRam, pLockedMem->u.phys.GCPhys, cb, fFlags, &pLockedMem->aPhysPages[0], pszDesc);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | /* Cleanup is done in VM destruction to which failure of this function will lead. */
|
---|
180 | /* Not true in case of MM_PHYS_TYPE_DYNALLOC_CHUNK */
|
---|
181 | }
|
---|
182 |
|
---|
183 | return rc;
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Relocate previously registered externally allocated RAM for the virtual machine.
|
---|
189 | *
|
---|
190 | * Use this only for MMIO ranges or the guest will become very confused.
|
---|
191 | * The memory registered with the VM thru this interface must not be freed
|
---|
192 | * before the virtual machine has been destroyed. Bad things may happen... :-)
|
---|
193 | *
|
---|
194 | * @return VBox status code.
|
---|
195 | * @param pVM VM handle.
|
---|
196 | * @param GCPhysOld The physical address the ram was registered at.
|
---|
197 | * @param GCPhysNew The physical address the ram shall be registered at.
|
---|
198 | * @param cb Size of the memory. Must be page aligend.
|
---|
199 | */
|
---|
200 | MMR3DECL(int) MMR3PhysRelocate(PVM pVM, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, unsigned cb)
|
---|
201 | {
|
---|
202 | Log(("MMR3PhysRelocate: GCPhysOld=%VGp GCPhysNew=%VGp cb=%#x\n", GCPhysOld, GCPhysNew, cb));
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * Validate input.
|
---|
206 | */
|
---|
207 | AssertMsg(pVM, ("Invalid VM pointer\n"));
|
---|
208 | AssertReturn(RT_ALIGN_T(GCPhysOld, PAGE_SIZE, RTGCPHYS) == GCPhysOld, VERR_INVALID_PARAMETER);
|
---|
209 | AssertReturn(RT_ALIGN_T(GCPhysNew, PAGE_SIZE, RTGCPHYS) == GCPhysNew, VERR_INVALID_PARAMETER);
|
---|
210 | AssertReturn(RT_ALIGN(cb, PAGE_SIZE) == cb, VERR_INVALID_PARAMETER);
|
---|
211 | RTGCPHYS GCPhysLast;
|
---|
212 | GCPhysLast = GCPhysOld + (cb - 1);
|
---|
213 | AssertReturn(GCPhysLast > GCPhysOld, VERR_INVALID_PARAMETER);
|
---|
214 | GCPhysLast = GCPhysNew + (cb - 1);
|
---|
215 | AssertReturn(GCPhysLast > GCPhysNew, VERR_INVALID_PARAMETER);
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Find the old memory region.
|
---|
219 | */
|
---|
220 | PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem;
|
---|
221 | while (pCur)
|
---|
222 | {
|
---|
223 | if ( pCur->eType == MM_LOCKED_TYPE_PHYS
|
---|
224 | && GCPhysOld == pCur->u.phys.GCPhys
|
---|
225 | && cb == pCur->cb)
|
---|
226 | break;
|
---|
227 |
|
---|
228 | /* next */
|
---|
229 | pCur = pCur->pNext;
|
---|
230 | }
|
---|
231 | if (!pCur)
|
---|
232 | {
|
---|
233 | AssertMsgFailed(("Unknown old region! %VGp LB%#x\n", GCPhysOld, cb));
|
---|
234 | return VERR_INVALID_PARAMETER;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Region is already locked, just need to change GC address.
|
---|
239 | */
|
---|
240 | /** @todo r=bird: check for conflicts? */
|
---|
241 | pCur->u.phys.GCPhys = GCPhysNew;
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Relocate the registered RAM range with PGM.
|
---|
245 | */
|
---|
246 | int rc = PGMR3PhysRelocate(pVM, GCPhysOld, GCPhysNew, cb);
|
---|
247 | if (VBOX_SUCCESS(rc))
|
---|
248 | {
|
---|
249 | /* Somewhat hackish way to relocate the region with REM. There
|
---|
250 | * is unfortunately no official way to unregister anything with
|
---|
251 | * REM, as there is no way to unregister memory with QEMU.
|
---|
252 | * This implementation seems to work, but is not very pretty. */
|
---|
253 | /// @todo one day provide a proper MMIO relocation operation
|
---|
254 | REMR3NotifyPhysReserve(pVM, GCPhysOld, cb);
|
---|
255 | REMR3NotifyPhysRamRegister(pVM, GCPhysNew, cb, pCur->pv,
|
---|
256 | pCur->aPhysPages[0].Phys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2));
|
---|
257 | }
|
---|
258 |
|
---|
259 | return rc;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Register a ROM (BIOS) region.
|
---|
265 | *
|
---|
266 | * It goes without saying that this is read-only memory. The memory region must be
|
---|
267 | * in unassigned memory. I.e. from the top of the address space or on the PC in
|
---|
268 | * the 0xa0000-0xfffff range.
|
---|
269 | *
|
---|
270 | * @returns VBox status.
|
---|
271 | * @param pVM VM Handle.
|
---|
272 | * @param pDevIns The device instance owning the ROM region.
|
---|
273 | * @param GCPhys First physical address in the range.
|
---|
274 | * Must be page aligned!
|
---|
275 | * @param cbRange The size of the range (in bytes).
|
---|
276 | * Must be page aligned!
|
---|
277 | * @param pvBinary Pointer to the binary data backing the ROM image.
|
---|
278 | * This must be cbRange bytes big.
|
---|
279 | * It will be copied and doesn't have to stick around.
|
---|
280 | * It will be copied and doesn't have to stick around if fShadow is clear.
|
---|
281 | * @param fShadow Whether to emulate ROM shadowing. This involves leaving
|
---|
282 | * the ROM writable for a while during the POST and refreshing
|
---|
283 | * it at reset. When this flag is set, the memory pointed to by
|
---|
284 | * pvBinary has to stick around for the lifespan of the VM.
|
---|
285 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
286 | * @remark There is no way to remove the rom, automatically on device cleanup or
|
---|
287 | * manually from the device yet. At present I doubt we need such features...
|
---|
288 | */
|
---|
289 | MMR3DECL(int) MMR3PhysRomRegister(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS GCPhys, RTUINT cbRange, const void *pvBinary,
|
---|
290 | bool fShadow, const char *pszDesc)
|
---|
291 | {
|
---|
292 | /*
|
---|
293 | * Validate input.
|
---|
294 | */
|
---|
295 | AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
|
---|
296 | AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
|
---|
297 | AssertReturn(RT_ALIGN(cbRange, PAGE_SIZE) == cbRange, VERR_INVALID_PARAMETER);
|
---|
298 | RTGCPHYS GCPhysLast = GCPhys + (cbRange - 1);
|
---|
299 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
300 | AssertPtrReturn(pvBinary, VERR_INVALID_PARAMETER);
|
---|
301 |
|
---|
302 |
|
---|
303 | /*
|
---|
304 | * Check if this can fit in an existing range.
|
---|
305 | *
|
---|
306 | * We do not handle the case where a new chunk of locked memory is
|
---|
307 | * required to accommodate the ROM since we assume MMR3PhysReserve()
|
---|
308 | * have been called to reserve the memory first.
|
---|
309 | *
|
---|
310 | * To make things even simpler, the pages in question must be
|
---|
311 | * marked as reserved.
|
---|
312 | */
|
---|
313 | PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem;
|
---|
314 | for ( ; pCur; pCur = pCur->pNext)
|
---|
315 | if ( pCur->eType == MM_LOCKED_TYPE_PHYS
|
---|
316 | && GCPhys - pCur->u.phys.GCPhys < pCur->cb)
|
---|
317 | break;
|
---|
318 | if (!pCur)
|
---|
319 | {
|
---|
320 | AssertMsgFailed(("No physical range was found matching the ROM location (%#VGp LB%#x)\n", GCPhys, cbRange));
|
---|
321 | return VERR_INVALID_PARAMETER;
|
---|
322 | }
|
---|
323 | if (GCPhysLast - pCur->u.phys.GCPhys >= pCur->cb)
|
---|
324 | {
|
---|
325 | AssertMsgFailed(("The ROM range (%#VGp LB%#x) was crossing the end of the physical range (%#VGp LB%#x)\n",
|
---|
326 | GCPhys, cbRange, pCur->u.phys.GCPhys, pCur->cb));
|
---|
327 | return VERR_INVALID_PARAMETER;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /* flags must be all reserved. */
|
---|
331 | unsigned iPage = (GCPhys - pCur->u.phys.GCPhys) >> PAGE_SHIFT;
|
---|
332 | unsigned iPageEnd = cbRange >> PAGE_SHIFT;
|
---|
333 | for (; iPage < iPageEnd; iPage++)
|
---|
334 | if ( (pCur->aPhysPages[iPage].Phys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2))
|
---|
335 | != MM_RAM_FLAGS_RESERVED)
|
---|
336 | {
|
---|
337 | AssertMsgFailed(("Flags conflict at %VGp, HCPhys=%VHp.\n", pCur->u.phys.GCPhys + (iPage << PAGE_SHIFT), pCur->aPhysPages[iPage].Phys));
|
---|
338 | return VERR_INVALID_PARAMETER;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * Copy the ram and update the flags.
|
---|
343 | */
|
---|
344 | iPage = (GCPhys - pCur->u.phys.GCPhys) >> PAGE_SHIFT;
|
---|
345 | void *pvCopy = (char *)pCur->pv + (iPage << PAGE_SHIFT);
|
---|
346 | memcpy(pvCopy, pvBinary, cbRange);
|
---|
347 |
|
---|
348 | const unsigned fSet = fShadow ? MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2 : MM_RAM_FLAGS_ROM;
|
---|
349 | for (; iPage < iPageEnd; iPage++)
|
---|
350 | {
|
---|
351 | pCur->aPhysPages[iPage].Phys &= ~MM_RAM_FLAGS_RESERVED;
|
---|
352 | pCur->aPhysPages[iPage].Phys |= fSet;
|
---|
353 | }
|
---|
354 | int rc = PGMR3PhysSetFlags(pVM, GCPhys, cbRange, fSet, ~MM_RAM_FLAGS_RESERVED);
|
---|
355 | AssertRC(rc);
|
---|
356 | if (VBOX_SUCCESS(rc))
|
---|
357 | {
|
---|
358 | /*
|
---|
359 | * To prevent the shadow page table mappings from being RW in raw-mode, we
|
---|
360 | * must currently employ a little hack. We register an write access handler
|
---|
361 | * and thereby ensures a RO mapping of the pages. This is NOT very nice,
|
---|
362 | * and wasn't really my intention when writing the code, consider it a PGM bug.
|
---|
363 | *
|
---|
364 | * ASSUMES that REMR3NotifyPhysRomRegister doesn't call cpu_register_physical_memory
|
---|
365 | * when there is no HC handler. The result would probably be immediate boot failure.
|
---|
366 | */
|
---|
367 | rc = PGMR3HandlerPhysicalRegister(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE, GCPhys, GCPhys + cbRange - 1,
|
---|
368 | NULL, NULL,
|
---|
369 | NULL, "pgmGuestROMWriteHandler", 0,
|
---|
370 | NULL, "pgmGuestROMWriteHandler", 0, pszDesc);
|
---|
371 | AssertRC(rc);
|
---|
372 | }
|
---|
373 |
|
---|
374 | /*
|
---|
375 | * Create a ROM range it so we can make a 'info rom' thingy and more importantly
|
---|
376 | * reload and protect/unprotect shadow ROM correctly.
|
---|
377 | */
|
---|
378 | if (VBOX_SUCCESS(rc))
|
---|
379 | {
|
---|
380 | PMMROMRANGE pRomRange = (PMMROMRANGE)MMR3HeapAlloc(pVM, MM_TAG_MM, sizeof(*pRomRange));
|
---|
381 | AssertReturn(pRomRange, VERR_NO_MEMORY);
|
---|
382 | pRomRange->GCPhys = GCPhys;
|
---|
383 | pRomRange->cbRange = cbRange;
|
---|
384 | pRomRange->pszDesc = pszDesc;
|
---|
385 | pRomRange->fShadow = fShadow;
|
---|
386 | pRomRange->fWritable = fShadow;
|
---|
387 | pRomRange->pvBinary = fShadow ? pvBinary : NULL;
|
---|
388 | pRomRange->pvCopy = pvCopy;
|
---|
389 |
|
---|
390 | /* sort it for 'info rom' readability. */
|
---|
391 | PMMROMRANGE pPrev = NULL;
|
---|
392 | PMMROMRANGE pCur = pVM->mm.s.pRomHead;
|
---|
393 | while (pCur && pCur->GCPhys < GCPhys)
|
---|
394 | {
|
---|
395 | pPrev = pCur;
|
---|
396 | pCur = pCur->pNext;
|
---|
397 | }
|
---|
398 | pRomRange->pNext = pCur;
|
---|
399 | if (pPrev)
|
---|
400 | pPrev->pNext = pRomRange;
|
---|
401 | else
|
---|
402 | pVM->mm.s.pRomHead = pRomRange;
|
---|
403 | }
|
---|
404 |
|
---|
405 | REMR3NotifyPhysRomRegister(pVM, GCPhys, cbRange, pvCopy, fShadow);
|
---|
406 | return rc; /* we're sloppy with error cleanup here, but we're toast anyway if this fails. */
|
---|
407 | }
|
---|
408 |
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * Reserve physical address space for ROM and MMIO ranges.
|
---|
412 | *
|
---|
413 | * @returns VBox status code.
|
---|
414 | * @param pVM VM Handle.
|
---|
415 | * @param GCPhys Start physical address.
|
---|
416 | * @param cbRange The size of the range.
|
---|
417 | * @param pszDesc Description string.
|
---|
418 | */
|
---|
419 | MMR3DECL(int) MMR3PhysReserve(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange, const char *pszDesc)
|
---|
420 | {
|
---|
421 | /*
|
---|
422 | * Validate input.
|
---|
423 | */
|
---|
424 | AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
|
---|
425 | AssertReturn(RT_ALIGN(cbRange, PAGE_SIZE) == cbRange, VERR_INVALID_PARAMETER);
|
---|
426 | RTGCPHYS GCPhysLast = GCPhys + (cbRange - 1);
|
---|
427 | AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
|
---|
428 |
|
---|
429 | /*
|
---|
430 | * Do we have an existing physical address range for the request?
|
---|
431 | */
|
---|
432 | PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem;
|
---|
433 | for ( ; pCur; pCur = pCur->pNext)
|
---|
434 | if ( pCur->eType == MM_LOCKED_TYPE_PHYS
|
---|
435 | && GCPhys - pCur->u.phys.GCPhys < pCur->cb)
|
---|
436 | break;
|
---|
437 | if (!pCur)
|
---|
438 | {
|
---|
439 | /*
|
---|
440 | * No range, we'll just allocate backing pages and register
|
---|
441 | * them as reserved using the Ram interface.
|
---|
442 | */
|
---|
443 | void *pvPages;
|
---|
444 | int rc = SUPPageAlloc(cbRange >> PAGE_SHIFT, &pvPages);
|
---|
445 | if (VBOX_SUCCESS(rc))
|
---|
446 | {
|
---|
447 | rc = MMR3PhysRegister(pVM, pvPages, GCPhys, cbRange, MM_RAM_FLAGS_RESERVED, pszDesc);
|
---|
448 | if (VBOX_FAILURE(rc))
|
---|
449 | SUPPageFree(pvPages, cbRange >> PAGE_SHIFT);
|
---|
450 | }
|
---|
451 | return rc;
|
---|
452 | }
|
---|
453 | if (GCPhysLast - pCur->u.phys.GCPhys >= pCur->cb)
|
---|
454 | {
|
---|
455 | AssertMsgFailed(("The reserved range (%#VGp LB%#x) was crossing the end of the physical range (%#VGp LB%#x)\n",
|
---|
456 | GCPhys, cbRange, pCur->u.phys.GCPhys, pCur->cb));
|
---|
457 | return VERR_INVALID_PARAMETER;
|
---|
458 | }
|
---|
459 |
|
---|
460 | /*
|
---|
461 | * Update the flags.
|
---|
462 | */
|
---|
463 | unsigned iPage = (GCPhys - pCur->u.phys.GCPhys) >> PAGE_SHIFT;
|
---|
464 | unsigned iPageEnd = cbRange >> PAGE_SHIFT;
|
---|
465 | for (; iPage < iPageEnd; iPage++)
|
---|
466 | pCur->aPhysPages[iPage].Phys |= MM_RAM_FLAGS_RESERVED;
|
---|
467 | int rc = PGMR3PhysSetFlags(pVM, GCPhys, cbRange, MM_RAM_FLAGS_RESERVED, ~0);
|
---|
468 | AssertRC(rc);
|
---|
469 |
|
---|
470 | REMR3NotifyPhysReserve(pVM, GCPhys, cbRange);
|
---|
471 | return rc;
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Get the size of the base RAM.
|
---|
477 | * This usually means the size of the first contigous block of physical memory.
|
---|
478 | *
|
---|
479 | * @returns The guest base RAM size.
|
---|
480 | * @param pVM The VM handle.
|
---|
481 | * @thread Any.
|
---|
482 | */
|
---|
483 | MMR3DECL(uint64_t) MMR3PhysGetRamSize(PVM pVM)
|
---|
484 | {
|
---|
485 | return pVM->mm.s.cbRamBase;
|
---|
486 | }
|
---|
487 |
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * Called by MMR3Reset to reset the shadow ROM.
|
---|
491 | *
|
---|
492 | * Resetting involves reloading the ROM into RAM and make it
|
---|
493 | * wriable again (as it was made read only at the end of the POST).
|
---|
494 | *
|
---|
495 | * @param pVM The VM handle.
|
---|
496 | */
|
---|
497 | void mmR3PhysRomReset(PVM pVM)
|
---|
498 | {
|
---|
499 | for (PMMROMRANGE pCur = pVM->mm.s.pRomHead; pCur; pCur = pCur->pNext)
|
---|
500 | if (pCur->fShadow)
|
---|
501 | {
|
---|
502 | memcpy(pCur->pvCopy, pCur->pvBinary, pCur->cbRange);
|
---|
503 | if (!pCur->fWritable)
|
---|
504 | {
|
---|
505 | int rc = PGMHandlerPhysicalDeregister(pVM, pCur->GCPhys);
|
---|
506 | AssertRC(rc);
|
---|
507 | pCur->fWritable = true;
|
---|
508 |
|
---|
509 | rc = PGMR3PhysSetFlags(pVM, pCur->GCPhys, pCur->cbRange, MM_RAM_FLAGS_MMIO2, ~0); /* ROM -> ROM + MMIO2 */
|
---|
510 | AssertRC(rc);
|
---|
511 |
|
---|
512 | REMR3NotifyPhysRomRegister(pVM, pCur->GCPhys, pCur->cbRange, pCur->pvCopy, true /* read-write now */);
|
---|
513 | }
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 |
|
---|
518 | /**
|
---|
519 | * Write-protects a shadow ROM range.
|
---|
520 | *
|
---|
521 | * This is called late in the POST for shadow ROM ranges.
|
---|
522 | *
|
---|
523 | * @returns VBox status code.
|
---|
524 | * @param pVM The VM handle.
|
---|
525 | * @param GCPhys Start of the registered shadow ROM range
|
---|
526 | * @param cbRange The length of the registered shadow ROM range.
|
---|
527 | * This can be NULL (not sure about the BIOS interface yet).
|
---|
528 | */
|
---|
529 | MMR3DECL(int) MMR3PhysRomProtect(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange)
|
---|
530 | {
|
---|
531 | for (PMMROMRANGE pCur = pVM->mm.s.pRomHead; pCur; pCur = pCur->pNext)
|
---|
532 | if ( pCur->GCPhys == GCPhys
|
---|
533 | && ( pCur->cbRange == cbRange
|
---|
534 | || !cbRange))
|
---|
535 | {
|
---|
536 | if (pCur->fWritable)
|
---|
537 | {
|
---|
538 | cbRange = pCur->cbRange;
|
---|
539 | int rc = PGMR3HandlerPhysicalRegister(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE, GCPhys, GCPhys + cbRange - 1,
|
---|
540 | NULL, NULL,
|
---|
541 | NULL, "pgmGuestROMWriteHandler", 0,
|
---|
542 | NULL, "pgmGuestROMWriteHandler", 0, pCur->pszDesc);
|
---|
543 | AssertRCReturn(rc, rc);
|
---|
544 | pCur->fWritable = false;
|
---|
545 |
|
---|
546 | rc = PGMR3PhysSetFlags(pVM, GCPhys, cbRange, 0, ~MM_RAM_FLAGS_MMIO2); /* ROM + MMIO2 -> ROM */
|
---|
547 | AssertRCReturn(rc, rc);
|
---|
548 | /* Don't bother with the MM page flags here because I don't think they are
|
---|
549 | really used beyond conflict checking at ROM, RAM, Reservation, etc. */
|
---|
550 |
|
---|
551 | REMR3NotifyPhysRomRegister(pVM, GCPhys, cbRange, pCur->pvCopy, false /* read-only now */);
|
---|
552 | }
|
---|
553 | return VINF_SUCCESS;
|
---|
554 | }
|
---|
555 | AssertMsgFailed(("GCPhys=%VGp cbRange=%#x\n", GCPhys, cbRange));
|
---|
556 | return VERR_INVALID_PARAMETER;
|
---|
557 | }
|
---|
558 |
|
---|