VirtualBox

source: vbox/trunk/src/VBox/VMM/MMPhys.cpp@ 4284

Last change on this file since 4284 was 4071, checked in by vboxsync, 18 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.9 KB
Line 
1/* $Id: MMPhys.cpp 4071 2007-08-07 17:07:59Z 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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * 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 */
51MMR3DECL(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 */
74MMR3DECL(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(ALIGNP(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.cbRAMSize += 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.cbRAMSize += 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 */
200MMR3DECL(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 * @param pszDesc Pointer to description string. This must not be freed.
281 * @remark There is no way to remove the rom, automatically on device cleanup or
282 * manually from the device yet. At present I doubt we need such features...
283 */
284MMR3DECL(int) MMR3PhysRomRegister(PVM pVM, PPDMDEVINS pDevIns, RTGCPHYS GCPhys, RTUINT cbRange, const void *pvBinary, const char *pszDesc)
285{
286 /*
287 * Validate input.
288 */
289 AssertPtrReturn(pDevIns, VERR_INVALID_PARAMETER);
290 AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
291 AssertReturn(RT_ALIGN(cbRange, PAGE_SIZE) == cbRange, VERR_INVALID_PARAMETER);
292 RTGCPHYS GCPhysLast = GCPhys + (cbRange - 1);
293 AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
294 AssertPtrReturn(pvBinary, VERR_INVALID_PARAMETER);
295
296
297 /*
298 * Check if this can fit in an existing range.
299 *
300 * We do not handle the case where a new chunk of locked memory is
301 * required to accommodate the ROM since we assume MMR3PhysReserve()
302 * have been called to reserve the memory first.
303 *
304 * To make things even simpler, the pages in question must be
305 * marked as reserved.
306 */
307 PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem;
308 for ( ; pCur; pCur = pCur->pNext)
309 if ( pCur->eType == MM_LOCKED_TYPE_PHYS
310 && GCPhys - pCur->u.phys.GCPhys < pCur->cb)
311 break;
312 if (!pCur)
313 {
314 AssertMsgFailed(("No physical range was found matching the ROM location (%#VGp LB%#x)\n", GCPhys, cbRange));
315 return VERR_INVALID_PARAMETER;
316 }
317 if (GCPhysLast - pCur->u.phys.GCPhys >= pCur->cb)
318 {
319 AssertMsgFailed(("The ROM range (%#VGp LB%#x) was crossing the end of the physical range (%#VGp LB%#x)\n",
320 GCPhys, cbRange, pCur->u.phys.GCPhys, pCur->cb));
321 return VERR_INVALID_PARAMETER;
322 }
323
324 /* flags must be all reserved. */
325 unsigned iPage = (GCPhys - pCur->u.phys.GCPhys) >> PAGE_SHIFT;
326 unsigned iPageEnd = cbRange >> PAGE_SHIFT;
327 for (; iPage < iPageEnd; iPage++)
328 if ( (pCur->aPhysPages[iPage].Phys & (MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_MMIO2))
329 != MM_RAM_FLAGS_RESERVED)
330 {
331 AssertMsgFailed(("Flags conflict at %VGp, HCPhys=%VHp.\n", pCur->u.phys.GCPhys + (iPage << PAGE_SHIFT), pCur->aPhysPages[iPage].Phys));
332 return VERR_INVALID_PARAMETER;
333 }
334
335 /*
336 * Copy the ram and update the flags.
337 */
338 iPage = (GCPhys - pCur->u.phys.GCPhys) >> PAGE_SHIFT;
339 void *pvCopy = (char *)pCur->pv + (iPage << PAGE_SHIFT);
340 memcpy(pvCopy, pvBinary, cbRange);
341
342 /** @note we rely on the MM_RAM_FLAGS_ROM flag in PGMPhysRead now. Don't change to reserved! */
343 /** @todo r=bird: Noone ever talked about changing *to* _RESERVED. The question is whether
344 * we should *clear* _RESERVED. I've no idea what the state of that flag is for ROM areas right
345 * now, but I will find out later. */
346 for (; iPage < iPageEnd; iPage++)
347 pCur->aPhysPages[iPage].Phys |= MM_RAM_FLAGS_ROM; /** @todo should be clearing _RESERVED? */
348 int rc = PGMR3PhysSetFlags(pVM, GCPhys, cbRange, MM_RAM_FLAGS_ROM, ~0); /** @todo should be clearing _RESERVED? */
349 AssertRC(rc);
350 if (VBOX_SUCCESS(rc))
351 {
352 /*
353 * Prevent changes to the ROM memory when executing in raw mode by
354 * registering a GC only write access handler.
355 *
356 * ASSUMES that REMR3NotifyPhysRomRegister doesn't call cpu_register_physical_memory
357 * when there is no HC handler. The result would probably be immediate boot failure.
358 */
359 rc = PGMR3HandlerPhysicalRegister(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE, GCPhys, GCPhys + cbRange - 1,
360 NULL, NULL,
361 NULL, "pgmGuestROMWriteHandler", 0,
362 NULL, "pgmGuestROMWriteHandler", 0, "ROM Write Access Handler");
363 AssertRC(rc);
364 }
365
366 REMR3NotifyPhysRomRegister(pVM, GCPhys, cbRange, pvCopy);
367 return rc; /* we're sloppy with error cleanup here, but we're toast anyway if this fails. */
368}
369
370
371/**
372 * Reserve physical address space for ROM and MMIO ranges.
373 *
374 * @returns VBox status code.
375 * @param pVM VM Handle.
376 * @param GCPhys Start physical address.
377 * @param cbRange The size of the range.
378 * @param pszDesc Description string.
379 */
380MMR3DECL(int) MMR3PhysReserve(PVM pVM, RTGCPHYS GCPhys, RTUINT cbRange, const char *pszDesc)
381{
382 /*
383 * Validate input.
384 */
385 AssertReturn(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys, VERR_INVALID_PARAMETER);
386 AssertReturn(RT_ALIGN(cbRange, PAGE_SIZE) == cbRange, VERR_INVALID_PARAMETER);
387 RTGCPHYS GCPhysLast = GCPhys + (cbRange - 1);
388 AssertReturn(GCPhysLast > GCPhys, VERR_INVALID_PARAMETER);
389
390 /*
391 * Do we have an existing physical address range for the request?
392 */
393 PMMLOCKEDMEM pCur = pVM->mm.s.pLockedMem;
394 for ( ; pCur; pCur = pCur->pNext)
395 if ( pCur->eType == MM_LOCKED_TYPE_PHYS
396 && GCPhys - pCur->u.phys.GCPhys < pCur->cb)
397 break;
398 if (!pCur)
399 {
400 /*
401 * No range, we'll just allocate backing pages and register
402 * them as reserved using the Ram interface.
403 */
404 void *pvPages;
405 int rc = SUPPageAlloc(cbRange >> PAGE_SHIFT, &pvPages);
406 if (VBOX_SUCCESS(rc))
407 {
408 rc = MMR3PhysRegister(pVM, pvPages, GCPhys, cbRange, MM_RAM_FLAGS_RESERVED, pszDesc);
409 if (VBOX_FAILURE(rc))
410 SUPPageFree(pvPages, cbRange >> PAGE_SHIFT);
411 }
412 return rc;
413 }
414 if (GCPhysLast - pCur->u.phys.GCPhys >= pCur->cb)
415 {
416 AssertMsgFailed(("The reserved range (%#VGp LB%#x) was crossing the end of the physical range (%#VGp LB%#x)\n",
417 GCPhys, cbRange, pCur->u.phys.GCPhys, pCur->cb));
418 return VERR_INVALID_PARAMETER;
419 }
420
421 /*
422 * Update the flags.
423 */
424 unsigned iPage = (GCPhys - pCur->u.phys.GCPhys) >> PAGE_SHIFT;
425 unsigned iPageEnd = cbRange >> PAGE_SHIFT;
426 for (; iPage < iPageEnd; iPage++)
427 pCur->aPhysPages[iPage].Phys |= MM_RAM_FLAGS_RESERVED;
428 int rc = PGMR3PhysSetFlags(pVM, GCPhys, cbRange, MM_RAM_FLAGS_RESERVED, ~0);
429 AssertRC(rc);
430
431 REMR3NotifyPhysReserve(pVM, GCPhys, cbRange);
432 return rc;
433}
434
435
436/**
437 * Get the size of the base RAM.
438 * This usually means the size of the first contigous block of physical memory.
439 *
440 * @returns
441 * @param pVM
442 * @thread Any.
443 */
444MMR3DECL(uint64_t) MMR3PhysGetRamSize(PVM pVM)
445{
446 return pVM->mm.s.cbRamBase;
447}
448
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette