MM.cpp revision 1c94c0a63ba68be1a7b2c640e70d7a06464e4fca
/* $Id$ */
/** @file
* MM - Memory Monitor(/Manager).
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
*
* WARNING: THIS IS SOMEWHAT OUTDATED!
*
* It seems like this is going to be the entity taking care of memory allocations
* and the locking of physical memory for a VM. MM will track these allocations and
* pinnings so pointer conversions, memory read and write, and correct clean up can
* be done.
*
* Memory types:
* - Hypervisor Memory Area (HMA).
* - Page tables.
* - Physical pages.
*
* The first two types are not accessible using the generic conversion functions
* for GC memory, there are special functions for these.
*
*
* A decent structure for this component need to be eveloped as we see usage. One
* or two rewrites is probabaly needed to get it right...
*
*
*
* @section Hypervisor Memory Area
*
* The hypervisor is give 4MB of space inside the guest, we assume that we can
* steal an page directory entry from the guest OS without cause trouble. In
* addition to these 4MB we'll be mapping memory for the graphics emulation,
* but that will be an independant mapping.
*
* The 4MBs are divided into two main parts:
* -# The static code and data
* -# The shortlived page mappings.
*
* The first part is used for the VM structure, the core code (VMMSwitch),
* GC modules, and the alloc-only-heap. The size will be determined at a
* later point but initially we'll say 2MB of locked memory, most of which
* is non contiguous physically.
*
* The second part is used for mapping pages to the hypervisor. We'll be using
* a simple round robin when doing these mappings. This means that no-one can
* assume that a mapping hangs around for very long, while the managing of the
* pages are very simple.
*
*
*
* @section Page Pool
*
* The MM manages a per VM page pool from which other components can allocate
* locked, page aligned and page granular memory objects. The pool provides
* facilities to convert back and forth between physical and virtual addresses
* (within the pool of course). Several specialized interfaces are provided
* for the most common alloctions and convertions to save the caller from
* bothersome casting and extra parameter passing.
*
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_MM
#include "MMInternal.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The current saved state versino of MM. */
#define MM_SAVED_STATE_VERSION 2
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Initializes the MM members of the UVM.
*
* This is currently only the ring-3 heap.
*
* @returns VBox status code.
* @param pUVM Pointer to the user mode VM structure.
*/
{
/*
* Assert sizes and order.
*/
/*
* Init the heap.
*/
}
/**
* Initializes the MM.
*
* MM is managing the virtual address space (among other things) and
* setup the hypvervisor memory area mapping in the VM structure and
* the hypvervisor alloc-only-heap. Assuming the current init order
* and components the hypvervisor memory area looks like this:
* -# VM Structure.
* -# Hypervisor alloc only heap (also call Hypervisor memory region).
* -# Core code.
*
* MM determins the virtual address of the hypvervisor memory area by
* checking for location at previous run. If that property isn't available
* it will choose a default starting location, currently 0xe0000000.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
LogFlow(("MMR3Init\n"));
/*
* Assert alignment, sizes and order.
*/
/*
* Init the structure.
*/
/*
* Init the page pool.
*/
if (VBOX_SUCCESS(rc))
{
/*
* Init the hypervisor related stuff.
*/
if (VBOX_SUCCESS(rc))
{
/*
* Register the saved state data unit.
*/
if (VBOX_SUCCESS(rc))
return rc;
/* .... failure .... */
}
}
return rc;
}
/**
* Initializes the MM parts which depends on PGM being initialized.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
* @remark No cleanup necessary since MMR3Term() will be called on failure.
*/
{
LogFlow(("MMR3InitPaging:\n"));
/*
* Query the CFGM values.
*/
int rc;
if (pMMCfg)
{
}
/** @cfgm{RamPreAlloc, boolean, false}
* Indicates whether the base RAM should all be allocated before starting
* the VM (default), or if it should be allocated when first written to.
*/
bool fPreAlloc;
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
fPreAlloc = false;
else
AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamPreAlloc\", rc=%Vrc.\n", rc), rc);
/** @cfgm{RamSize, uint64_t, 0, 0, UINT64_MAX}
* Specifies the size of the base RAM that is to be set up during
* VM initialization.
*/
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
cbRam = 0;
else
AssertMsgRCReturn(rc, ("Configuration error: Failed to query integer \"RamSize\", rc=%Vrc.\n", rc), rc);
pVM->mm.s.cbRamBase = cbRam; /* Warning: don't move this code to MMR3Init without fixing REMR3Init. */
* Specifies the policy to use when reserving memory for this VM. The recognized
* value is 'no overcommitment' (default). See GMMPOLICY.
*/
char sz[64];
if (RT_SUCCESS(rc))
{
else
return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Policy\" value \"%s\"", sz);
}
else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else
AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Policy\", rc=%Vrc.\n", rc), rc);
* Specifies the memory priority of this VM. The priority comes into play when the
* system is overcommitted and the VMs needs to be milked for memory. The recognized
* values are 'low', 'normal' (default) and 'high'. See GMMPRIORITY.
*/
if (RT_SUCCESS(rc))
{
else
return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS, "Unknown \"MM/Priority\" value \"%s\"", sz);
}
else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else
AssertMsgRCReturn(rc, ("Configuration error: Failed to query string \"MM/Priority\", rc=%Vrc.\n", rc), rc);
/*
* Make the initial memory reservation with GMM.
*/
if (RT_FAILURE(rc))
{
N_("Insufficient free memory to start the VM (cbRam=%#RX64 enmPolicy=%d enmPriority=%d)"),
}
/*
* If RamSize is 0 we're done now.
*/
{
Log(("MM: No RAM configured\n"));
return VINF_SUCCESS;
}
/*
* Setup the base ram (PGM).
*/
#ifdef VBOX_WITH_NEW_PHYS_CODE
{
/** @todo RamPreAlloc should be handled at the very end of the VM creation. (lazy bird) */
}
#else
if (RT_SUCCESS(rc))
{
/*
* Allocate the first chunk, as we'll map ROM ranges there.
* If requested, allocated the rest too.
*/
for (GCPhys = PGM_DYNAMIC_CHUNK_SIZE;
}
#endif
return rc;
}
/**
* Terminates the MM.
*
* Termination means cleaning up and freeing all resources,
* the VM it self is at this point powered off or suspended.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
/*
* Destroy the page pool. (first as it used the hyper heap)
*/
/*
* Release locked memory.
* (Associated record are released by the heap.)
*/
while (pLockedMem)
{
switch (pLockedMem->eType)
{
case MM_LOCKED_TYPE_HYPER:
break;
case MM_LOCKED_TYPE_PHYS:
/* nothing to do. */
break;
}
/* next */
}
/*
* Zero stuff to detect after termination use of the MM interface
*/
return 0;
}
/**
* Terminates the UVM part of MM.
*
* Termination means cleaning up and freeing all resources,
* the VM it self is at this point powered off or suspended.
*
* @returns VBox status code.
* @param pUVM Pointer to the user mode VM structure.
*/
{
/*
* Destroy the heap.
*/
}
/**
* Reset notification.
*
* MM will reload shadow ROMs into RAM at this point and make
* the ROM writable.
*
* @param pVM The VM handle.
*/
{
}
/**
* Execute state save operation.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param pSSM SSM operation handle.
*/
{
LogFlow(("mmR3Save:\n"));
/* (PGM saves the physical memory.) */
}
/**
* Execute state load operation.
*
* @returns VBox status code.
* @param pVM VM Handle.
* @param pSSM SSM operation handle.
* @param u32Version Data layout version.
*/
{
LogFlow(("mmR3Load:\n"));
/*
* Validate version.
*/
|| !u32Version)
{
}
/*
* Check the cBasePages and cbRamBase values.
*/
int rc;
/* cBasePages */
if (u32Version != 1)
else
{
}
if (VBOX_FAILURE(rc))
return rc;
{
Log(("mmR3Load: Memory configuration has changed. cPages=%#RX64 saved=%#RX64\n", pVM->mm.s.cBasePages, cPages));
}
/* cbRamBase */
if (u32Version != 1)
else
{
}
if (VBOX_FAILURE(rc))
return rc;
{
Log(("mmR3Load: Memory configuration has changed. cbRamBase=%#RX64 save=%#RX64\n", pVM->mm.s.cbRamBase, cb));
}
/* (PGM restores the physical memory.) */
return rc;
}
/**
* Updates GMM with memory reservation changes.
*
* Called when MM::cbRamRegistered, MM::cShadowPages or MM::cFixedPages changes.
*
* @returns VBox status code - see GMMR0UpdateReservation.
* @param pVM The shared VM structure.
*/
{
return GMMR3UpdateReservation(pVM,
return VINF_SUCCESS;
}
/**
* Interface for PGM to increase the reservation of RAM and ROM pages.
*
* This can be called before MMR3InitPaging.
*
* @returns VBox status code. Will set VM error on failure.
* @param pVM The shared VM structure.
* @param cAddBasePages The number of pages to add.
*/
{
LogFlow(("MMR3IncreaseBaseReservation: +%RU64 (%RU64 -> %RU64\n", cAddBasePages, cOld, pVM->mm.s.cBasePages));
if (RT_FAILURE(rc))
{
VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserved physical memory for the RAM (%#RX64 -> %#RX64)"), cOld, pVM->mm.s.cBasePages);
}
return rc;
}
/**
* Interface for PGM to adjust the reservation of fixed pages.
*
* This can be called before MMR3InitPaging.
*
* @returns VBox status code. Will set VM error on failure.
* @param pVM The shared VM structure.
* @param cDeltaFixedPages The number of pages to add (positive) or subtract (negative).
* @param pszDesc Some description associated with the reservation.
*/
{
LogFlow(("MMR3AdjustFixedReservation: %d (%u -> %u)\n", cDeltaFixedPages, cOld, pVM->mm.s.cFixedPages));
if (RT_FAILURE(rc))
{
}
return rc;
}
/**
* Interface for PGM to update the reservation of shadow pages.
*
* This can be called before MMR3InitPaging.
*
* @returns VBox status code. Will set VM error on failure.
* @param pVM The shared VM structure.
* @param cShadowPages The new page count.
*/
{
if (RT_FAILURE(rc))
{
VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to reserve physical memory for shadow page tables (%#x -> %#x)"), cOld, pVM->mm.s.cShadowPages);
}
return rc;
}
/**
* Locks physical memory which backs a virtual memory range (HC) adding
* the required records to the pLockedMem list.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pv Pointer to memory range which shall be locked down.
* This pointer is page aligned.
* @param cb Size of memory range (in bytes). This size is page aligned.
* @param eType Memory type.
* @param ppLockedMem Where to store the pointer to the created locked memory record.
* This is optional, pass NULL if not used.
* @param fSilentFailure Don't raise an error when unsuccessful. Upper layer with deal with it.
*/
int mmR3LockMem(PVM pVM, void *pv, size_t cb, MMLOCKEDTYPE eType, PMMLOCKEDMEM *ppLockedMem, bool fSilentFailure)
{
if (ppLockedMem)
*ppLockedMem = NULL;
/*
* Allocate locked mem structure.
*/
PMMLOCKEDMEM pLockedMem = (PMMLOCKEDMEM)MMR3HeapAlloc(pVM, MM_TAG_MM, RT_OFFSETOF(MMLOCKEDMEM, aPhysPages[cPages]));
if (!pLockedMem)
return VERR_NO_MEMORY;
/*
* Lock the memory.
*/
if (VBOX_SUCCESS(rc))
{
/*
* Setup the reserved field.
*/
/*
* Insert into the list.
*
* ASSUME no protected needed here as only one thread in the system can possibly
* be doing this. No other threads will walk this list either we assume.
*/
/* Set return value. */
if (ppLockedMem)
}
else
{
if (!fSilentFailure)
rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to lock %d bytes of host memory (out of memory)"), cb);
}
return rc;
}
/**
* Maps a part of or an entire locked memory region into the guest context.
*
* @returns VBox status.
* God knows what happens if we fail...
* @param pVM VM handle.
* @param pLockedMem Locked memory structure.
* @param Addr GC Address where to start the mapping.
* @param iPage Page number in the locked memory region.
* @param cPages Number of pages to map.
* @param fFlags See the fFlags argument of PGR3Map().
*/
int mmR3MapLocked(PVM pVM, PMMLOCKEDMEM pLockedMem, RTGCPTR Addr, unsigned iPage, size_t cPages, unsigned fFlags)
{
/*
* Adjust ~0 argument
*/
/* no incorrect arguments are accepted */
AssertMsg(iPage < (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad iPage(=%d)\n", iPage));
AssertMsg(iPage + cPages <= (pLockedMem->cb >> PAGE_SHIFT), ("never even think about giving me a bad cPages(=%d)\n", cPages));
/*
* Map the the pages.
*/
while (cPages)
{
if (VBOX_FAILURE(rc))
{
/** @todo how the hell can we do a proper bailout here. */
return rc;
}
/* next */
cPages--;
iPage++;
pPhysPage++;
}
return VINF_SUCCESS;
}
/**
* Convert HC Physical address to HC Virtual address.
*
* @returns VBox status.
* @param pVM VM handle.
* @param HCPhys The host context virtual address.
* @param ppv Where to store the resulting address.
* @thread The Emulation Thread.
*/
{
/*
* Try page tables.
*/
if (VBOX_SUCCESS(rc))
return rc;
/*
* Iterate the locked memory - very slow.
*/
{
while (iPage-- > 0)
{
return VINF_SUCCESS;
}
}
/* give up */
return VERR_INVALID_POINTER;
}
/**
* Read memory from GC virtual address using the current guest CR3.
*
* @returns VBox status.
* @param pVM VM handle.
* @param pvDst Destination address (HC of course).
* @param GCPtr GC virtual address.
* @param cb Number of bytes to read.
*/
{
}
/**
* Write to memory at GC virtual address translated using the current guest CR3.
*
* @returns VBox status.
* @param pVM VM handle.
* @param GCPtrDst GC virtual address.
* @param pvSrc The source address (HC of course).
* @param cb Number of bytes to read.
*/
{
return VERR_ACCESS_DENIED;
}