GMMR0.cpp revision b94e998d08ac3d6a72832557f92942f6c7fdae81
/* $Id$ */
/** @file
* GMM - Global Memory Manager.
*/
/*
* Copyright (C) 2007-2011 Oracle Corporation
*
* 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.
*/
/** @page pg_gmm GMM - The Global Memory Manager
*
* As the name indicates, this component is responsible for global memory
* management. Currently only guest RAM is allocated from the GMM, but this
* may change to include shadow page tables and other bits later.
*
* Guest RAM is managed as individual pages, but allocated from the host OS
* in chunks for reasons of portability / efficiency. To minimize the memory
* footprint all tracking structure must be as small as possible without
* unnecessary performance penalties.
*
* The allocation chunks has fixed sized, the size defined at compile time
* by the #GMM_CHUNK_SIZE \#define.
*
* Each chunk is given an unique ID. Each page also has a unique ID. The
* relation ship between the two IDs is:
* @code
* GMM_CHUNK_SHIFT = log2(GMM_CHUNK_SIZE / PAGE_SIZE);
* idPage = (idChunk << GMM_CHUNK_SHIFT) | iPage;
* @endcode
* Where iPage is the index of the page within the chunk. This ID scheme
* permits for efficient chunk and page lookup, but it relies on the chunk size
* to be set at compile time. The chunks are organized in an AVL tree with their
* IDs being the keys.
*
* The physical address of each page in an allocation chunk is maintained by
* the #RTR0MEMOBJ and obtained using #RTR0MemObjGetPagePhysAddr. There is no
* need to duplicate this information (it'll cost 8-bytes per page if we did).
*
* So what do we need to track per page? Most importantly we need to know
* which state the page is in:
* - Private - Allocated for (eventually) backing one particular VM page.
* - Shared - Readonly page that is used by one or more VMs and treated
* as COW by PGM.
* - Free - Not used by anyone.
*
* For the page replacement operations (sharing, defragmenting and freeing)
* to be somewhat efficient, private pages needs to be associated with a
* particular page in a particular VM.
*
* Tracking the usage of shared pages is impractical and expensive, so we'll
* settle for a reference counting system instead.
*
* Free pages will be chained on LIFOs
*
* On 64-bit systems we will use a 64-bit bitfield per page, while on 32-bit
* systems a 32-bit bitfield will have to suffice because of address space
* limitations. The #GMMPAGE structure shows the details.
*
*
* @section sec_gmm_alloc_strat Page Allocation Strategy
*
* The strategy for allocating pages has to take fragmentation and shared
* pages into account, or we may end up with with 2000 chunks with only
* a few pages in each. Shared pages cannot easily be reallocated because
* of the inaccurate usage accounting (see above). Private pages can be
* reallocated by a defragmentation thread in the same manner that sharing
* is done.
*
* The first approach is to manage the free pages in two sets depending on
* whether they are mainly for the allocation of shared or private pages.
* In the initial implementation there will be almost no possibility for
* mixing shared and private pages in the same chunk (only if we're really
* stressed on memory), but when we implement forking of VMs and have to
* deal with lots of COW pages it'll start getting kind of interesting.
*
* The sets are lists of chunks with approximately the same number of
* free pages. Say the chunk size is 1MB, meaning 256 pages, and a set
* consists of 16 lists. So, the first list will contain the chunks with
* 1-7 free pages, the second covers 8-15, and so on. The chunks will be
* moved between the lists as pages are freed up or allocated.
*
*
* @section sec_gmm_costs Costs
*
* The per page cost in kernel space is 32-bit plus whatever RTR0MEMOBJ
* entails. In addition there is the chunk cost of approximately
* (sizeof(RT0MEMOBJ) + sizeof(CHUNK)) / 2^CHUNK_SHIFT bytes per page.
*
* On Windows the per page #RTR0MEMOBJ cost is 32-bit on 32-bit windows
* and 64-bit on 64-bit windows (a PFN_NUMBER in the MDL). So, 64-bit per page.
* The cost on Linux is identical, but here it's because of sizeof(struct page *).
*
*
* @section sec_gmm_legacy Legacy Mode for Non-Tier-1 Platforms
*
* In legacy mode the page source is locked user pages and not
* #RTR0MemObjAllocPhysNC, this means that a page can only be allocated
* by the VM that locked it. We will make no attempt at implementing
* page sharing on these systems, just do enough to make it all work.
*
*
* @subsection sub_gmm_locking Serializing
*
* One simple fast mutex will be employed in the initial implementation, not
* two as mentioned in @ref subsec_pgmPhys_Serializing.
*
* @see @ref subsec_pgmPhys_Serializing
*
*
* @section sec_gmm_overcommit Memory Over-Commitment Management
*
* The GVM will have to do the system wide memory over-commitment
* management. My current ideas are:
* - Per VM oc policy that indicates how much to initially commit
* to it and what to do in a out-of-memory situation.
* - Prevent overtaxing the host.
*
* There are some challenges here, the main ones are configurability and
* security. Should we for instance permit anyone to request 100% memory
* commitment? Who should be allowed to do runtime adjustments of the
* config. And how to prevent these settings from being lost when the last
* VM process exits? The solution is probably to have an optional root
* daemon the will keep VMMR0.r0 in memory and enable the security measures.
*
*
*
* @section sec_gmm_numa NUMA
*
* NUMA considerations will be designed and implemented a bit later.
*
* The preliminary guesses is that we will have to try allocate memory as
* close as possible to the CPUs the VM is executed on (EMT and additional CPU
* threads). Which means it's mostly about allocation and sharing policies.
* Both the scheduler and allocator interface will to supply some NUMA info
* and we'll need to have a way to calc access costs.
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_GMM
#include "GMMR0Internal.h"
#include <iprt/semaphore.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Pointer to set of free chunks. */
typedef struct GMMCHUNKFREESET *PGMMCHUNKFREESET;
/**
* The per-page tracking structure employed by the GMM.
*
* On 32-bit hosts we'll some trickery is necessary to compress all
* the information into 32-bits. When the fSharedFree member is set,
* the 30th bit decides whether it's a free page or not.
*
* Because of the different layout on 32-bit and 64-bit hosts, macros
* are used to get and set some of the data.
*/
typedef union GMMPAGE
{
#if HC_ARCH_BITS == 64
/** Unsigned integer view. */
uint64_t u;
/** The common view. */
struct GMMPAGECOMMON
{
/** The page state. */
} Common;
/** The view of a private page. */
struct GMMPAGEPRIVATE
{
/** The guest page frame number. (Max addressable: 2 ^ 44 - 16) */
/** The GVM handle. (64K VMs) */
/** Reserved. */
/** The page state. */
} Private;
/** The view of a shared page. */
struct GMMPAGESHARED
{
/** The host page frame number. (Max addressable: 2 ^ 44 - 16) */
/** The reference count (64K VMs). */
/** Reserved. Checksum or something? Two hGVMs for forking? */
/** The page state. */
} Shared;
/** The view of a free page. */
struct GMMPAGEFREE
{
/** The index of the next page in the free list. UINT16_MAX is NIL. */
/** Reserved. Checksum or something? */
/** Reserved. Checksum or something? */
/** The page state. */
} Free;
#else /* 32-bit */
/** Unsigned integer view. */
uint32_t u;
/** The common view. */
struct GMMPAGECOMMON
{
/** The page state. */
} Common;
/** The view of a private page. */
struct GMMPAGEPRIVATE
{
/** The guest page frame number. (Max addressable: 2 ^ 36) */
/** The GVM handle. (127 VMs) */
/** The top page state bit, MBZ. */
} Private;
/** The view of a shared page. */
struct GMMPAGESHARED
{
/** The reference count. */
/** The page state. */
} Shared;
/** The view of a free page. */
struct GMMPAGEFREE
{
/** The index of the next page in the free list. UINT16_MAX is NIL. */
/** Reserved. Checksum or something? */
/** The page state. */
} Free;
#endif
} GMMPAGE;
/** Pointer to a GMMPAGE. */
/** @name The Page States.
* @{ */
/** A private page. */
#define GMM_PAGE_STATE_PRIVATE 0
/** A private page - alternative value used on the 32-bit implementation.
* This will never be used on 64-bit hosts. */
#define GMM_PAGE_STATE_PRIVATE_32 1
/** A shared page. */
#define GMM_PAGE_STATE_SHARED 2
/** A free page. */
#define GMM_PAGE_STATE_FREE 3
/** @} */
/** @def GMM_PAGE_IS_PRIVATE
*
* @returns true if private, false if not.
* @param pPage The GMM page.
*/
#if HC_ARCH_BITS == 64
#else
#endif
/** @def GMM_PAGE_IS_SHARED
*
* @returns true if shared, false if not.
* @param pPage The GMM page.
*/
/** @def GMM_PAGE_IS_FREE
*
* @returns true if free, false if not.
* @param pPage The GMM page.
*/
/** @def GMM_PAGE_PFN_LAST
* The last valid guest pfn range.
* @remark Some of the values outside the range has special meaning,
* see GMM_PAGE_PFN_UNSHAREABLE.
*/
#if HC_ARCH_BITS == 64
#else
#endif
/** @def GMM_PAGE_PFN_UNSHAREABLE
* Indicates that this page isn't used for normal guest memory and thus isn't shareable.
*/
#if HC_ARCH_BITS == 64
#else
#endif
/**
* A GMM allocation chunk ring-3 mapping record.
*
* This should really be associated with a session and not a VM, but
* it's simpler to associated with a VM and cleanup with the VM object
* is destroyed.
*/
typedef struct GMMCHUNKMAP
{
/** The mapping object. */
/** The VM owning the mapping. */
} GMMCHUNKMAP;
/** Pointer to a GMM allocation chunk mapping. */
typedef struct GMMCHUNKMAP *PGMMCHUNKMAP;
/**
* A GMM allocation chunk.
*/
typedef struct GMMCHUNK
{
/** The AVL node core.
* The Key is the chunk ID. (Giant mtx.) */
/** The memory object.
* Either from RTR0MemObjAllocPhysNC or RTR0MemObjLockUser depending on
* what the host can dish up with. (Chunk mtx protects mapping accesses
* and related frees.) */
/** Pointer to the next chunk in the free list. (Giant mtx.) */
/** Pointer to the previous chunk in the free list. (Giant mtx.) */
/** Pointer to the free set this chunk belongs to. NULL for
* chunks with no free pages. (Giant mtx.) */
/** List node in the chunk list (GMM::ChunkList). (Giant mtx.) */
/** Pointer to an array of mappings. (Chunk mtx.) */
/** The number of mappings. (Chunk mtx.) */
/** The mapping lock this chunk is using using. UINT16_MAX if nobody is
* mapping or freeing anything. (Giant mtx.) */
/** Flags field reserved for future use (like eliminating enmType).
* (Giant mtx.) */
/** The head of the list of free pages. UINT16_MAX is the NIL value.
* (Giant mtx.) */
/** The number of free pages. (Giant mtx.) */
/** The GVM handle of the VM that first allocated pages from this chunk, this
* is used as a preference when there are several chunks to choose from.
* When in bound memory mode this isn't a preference any longer. (Giant
* mtx.) */
/** The ID of the NUMA node the memory mostly resides on. (Reserved for
* future use.) (Giant mtx.) */
/** The number of private pages. (Giant mtx.) */
/** The number of shared pages. (Giant mtx.) */
/** The pages. (Giant mtx.) */
} GMMCHUNK;
/** Indicates that the NUMA properies of the memory is unknown. */
/** @name GMM_CHUNK_FLAGS_XXX - chunk flags.
* @{ */
/** Indicates that the chunk is a large page (2MB). */
/** @} */
/**
* An allocation chunk TLB entry.
*/
typedef struct GMMCHUNKTLBE
{
/** The chunk id. */
/** Pointer to the chunk. */
} GMMCHUNKTLBE;
/** Pointer to an allocation chunk TLB entry. */
typedef GMMCHUNKTLBE *PGMMCHUNKTLBE;
/** The number of entries tin the allocation chunk TLB. */
#define GMM_CHUNKTLB_ENTRIES 32
/** Gets the TLB entry index for the given Chunk ID. */
/**
* An allocation chunk TLB.
*/
typedef struct GMMCHUNKTLB
{
/** The TLB entries. */
} GMMCHUNKTLB;
/** Pointer to an allocation chunk TLB. */
typedef GMMCHUNKTLB *PGMMCHUNKTLB;
/**
* The GMM instance data.
*/
typedef struct GMM
{
/** Magic / eye catcher. GMM_MAGIC */
/** The number of threads waiting on the mutex. */
/** The fast mutex protecting the GMM.
* More fine grained locking can be implemented later if necessary. */
#ifdef VBOX_STRICT
/** The current mutex owner. */
#endif
/** The chunk tree. */
/** The chunk TLB. */
/** The private free set. */
/** The shared free set. */
/** Shared module tree (global). */
/** @todo separate trees for distinctly different guest OSes. */
/** The chunk list. For simplifying the cleanup process. */
/** The maximum number of pages we're allowed to allocate.
/** The number of pages that has been reserved.
* The deal is that cReservedPages - cOverCommittedPages <= cMaxPages. */
/** The number of pages that we have over-committed in reservations. */
/** The number of actually allocated (committed if you like) pages. */
/** The number of pages that are shared. A subset of cAllocatedPages. */
/** The number of pages that are actually shared between VMs. */
/** The number of pages that are shared that has been left behind by
* VMs not doing proper cleanups. */
/** The number of allocation chunks.
* (The number of pages we've allocated from the host can be derived from this.) */
/** The number of current ballooned pages. */
/** The legacy allocation mode indicator.
* This is determined at initialization time. */
bool fLegacyAllocationMode;
/** The bound memory mode indicator.
* When set, the memory will be bound to a specific VM and never
* shared. This is always set if fLegacyAllocationMode is set.
* (Also determined at initialization time.) */
bool fBoundMemoryMode;
/** The number of registered VMs. */
/** The number of freed chunks ever. This is used a list generation to
* avoid restarting the cleanup scanning when the list wasn't modified. */
/** The previous allocated Chunk ID.
* Used as a hint to avoid scanning the whole bitmap. */
/** Chunk ID allocation bitmap.
* Bits of allocated IDs are set, free ones are clear.
* The NIL id (0) is marked allocated. */
/** The index of the next mutex to use. */
/** Chunk locks for reducing lock contention without having to allocate
* one lock per chunk. */
struct
{
/** The mutex */
/** The number of threads currently using this mutex. */
} aChunkMtx[64];
} GMM;
/** Pointer to the GMM instance. */
/** The value of GMM::u32Magic (Katsuhiro Otomo). */
/**
* GMM chunk mutex state.
*
* This is returned by gmmR0ChunkMutexAcquire and is used by the other
* gmmR0ChunkMutex* methods.
*/
typedef struct GMMR0CHUNKMTXSTATE
{
/** The index of the chunk mutex. */
/** The relevant flags (GMMR0CHUNK_MTX_XXX). */
/** Pointer to a chunk mutex state. */
typedef GMMR0CHUNKMTXSTATE *PGMMR0CHUNKMTXSTATE;
/** @name GMMR0CHUNK_MTX_XXX
* @{ */
#define GMMR0CHUNK_MTX_INVALID UINT32_C(0)
/** @} */
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Pointer to the GMM instance data. */
/** Macro for obtaining and validating the g_pGMM pointer.
* On failure it will return from the invoking function with the specified return value.
*
* @param pGMM The name of the pGMM variable.
* @param rc The return value on failure. Use VERR_INTERNAL_ERROR for
* VBox status codes.
*/
do { \
} while (0)
/** Macro for obtaining and validating the g_pGMM pointer, void function variant.
* On failure it will return from the invoking function.
*
* @param pGMM The name of the pGMM variable.
*/
#define GMM_GET_VALID_INSTANCE_VOID(pGMM) \
do { \
AssertPtrReturnVoid((pGMM)); \
} while (0)
/** @def GMM_CHECK_SANITY_UPON_ENTERING
* Checks the sanity of the GMM instance data before making changes.
*
* This is macro is a stub by default and must be enabled manually in the code.
*
* @returns true if sane, false if not.
* @param pGMM The name of the pGMM variable.
*/
#if defined(VBOX_STRICT) && 0
# define GMM_CHECK_SANITY_UPON_ENTERING(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
#else
# define GMM_CHECK_SANITY_UPON_ENTERING(pGMM) (true)
#endif
/** @def GMM_CHECK_SANITY_UPON_LEAVING
* Checks the sanity of the GMM instance data after making changes.
*
* This is macro is a stub by default and must be enabled manually in the code.
*
* @returns true if sane, false if not.
* @param pGMM The name of the pGMM variable.
*/
#if defined(VBOX_STRICT) && 0
# define GMM_CHECK_SANITY_UPON_LEAVING(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
#else
# define GMM_CHECK_SANITY_UPON_LEAVING(pGMM) (true)
#endif
/** @def GMM_CHECK_SANITY_IN_LOOPS
* Checks the sanity of the GMM instance in the allocation loops.
*
* This is macro is a stub by default and must be enabled manually in the code.
*
* @returns true if sane, false if not.
* @param pGMM The name of the pGMM variable.
*/
#if defined(VBOX_STRICT) && 0
# define GMM_CHECK_SANITY_IN_LOOPS(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
#else
# define GMM_CHECK_SANITY_IN_LOOPS(pGMM) (true)
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Initializes the GMM component.
*
* This is called when the VMMR0.r0 module is loaded and protected by the
* loader semaphore.
*
* @returns VBox status code.
*/
{
LogFlow(("GMMInit:\n"));
/*
* Allocate the instance data and the locks.
*/
if (!pGMM)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
unsigned iMtx;
{
if (RT_FAILURE(rc))
break;
}
if (RT_SUCCESS(rc))
{
/*
* Check and see if RTR0MemObjAllocPhysNC works.
*/
#if 0 /* later, see #3170. */
if (RT_SUCCESS(rc))
{
}
else if (rc == VERR_NOT_SUPPORTED)
else
#else
# if defined(RT_OS_WINDOWS) || (defined(RT_OS_SOLARIS) && ARCH_BITS == 64) || defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
pGMM->fLegacyAllocationMode = false;
# if ARCH_BITS == 32
/* Don't reuse possibly partial chunks because of the virtual
address space limitation. */
pGMM->fBoundMemoryMode = true;
# else
pGMM->fBoundMemoryMode = false;
# endif
# else
pGMM->fLegacyAllocationMode = true;
pGMM->fBoundMemoryMode = true;
# endif
#endif
/*
* Query system page count and guess a reasonable cMaxPages value.
*/
LogFlow(("GMMInit: pGMM=%p fLegacyAllocationMode=%RTbool fBoundMemoryMode=%RTbool\n", pGMM, pGMM->fLegacyAllocationMode, pGMM->fBoundMemoryMode));
return VINF_SUCCESS;
}
/*
* Bail out.
*/
while (iMtx-- > 0)
}
return rc;
}
/**
* Terminates the GMM component.
*/
{
LogFlow(("GMMTerm:\n"));
/*
* Take care / be paranoid...
*/
return;
{
return;
}
/*
* Undo what init did and free all the resources we've acquired.
*/
/* Destroy the fundamentals. */
/* Free any chunks still hanging around. */
/* Destroy the chunk locks. */
{
}
/* Finally the instance data itself. */
LogFlow(("GMMTerm: done\n"));
}
/**
* RTAvlU32Destroy callback.
*
* @returns 0
* @param pNode The node to destroy.
* @param pvGMM The GMM handle.
*/
{
if (RT_FAILURE(rc))
{
}
return 0;
}
/**
* Initializes the per-VM data for the GMM.
*
* This is called from within the GVMM lock (from GVMMR0CreateVM)
* and should only initialize the data members so GMMR0CleanupVM
* can deal with them. We reserve no memory or anything here,
* that's done later in GMMR0InitVM.
*
* @param pGVM Pointer to the Global VM structure.
*/
{
}
/**
* Acquires the GMM giant lock.
*
* @returns Assert status code from RTSemFastMutexRequest.
* @param pGMM Pointer to the GMM instance.
*/
{
#ifdef VBOX_STRICT
#endif
return rc;
}
/**
* Releases the GMM giant lock.
*
* @returns Assert status code from RTSemFastMutexRequest.
* @param pGMM Pointer to the GMM instance.
*/
{
#ifdef VBOX_STRICT
#endif
return rc;
}
/**
* Yields the GMM giant lock if there is contention and a certain minimum time
* has elapsed since we took it.
*
* @returns @c true if the mutex was yielded, @c false if not.
* @param pGMM Pointer to the GMM instance.
* @param puLockNanoTS Where the lock acquisition time stamp is kept
*/
{
/*
* If nobody is contending the mutex, don't bother checking the time.
*/
return false;
/*
* Don't yield if we haven't executed for at least 2 milliseconds.
*/
return false;
/*
* Yield the mutex.
*/
#ifdef VBOX_STRICT
#endif
#ifdef VBOX_STRICT
#endif
return true;
}
/**
* Acquires a chunk lock.
*
* The caller must own the giant lock.
*
* @returns Assert status code from RTSemFastMutexRequest.
* @param pMtxState The chunk mutex state info. (Avoids
* passing the same flags and stuff around
* for subsequent release and drop-giant
* calls.)
* @param pGMM Pointer to the GMM instance.
* @param pChunk Pointer to the chunk.
* @param fFlags Flags regarding the giant lock, GMMR0CHUNK_MTX_XXX.
*/
static int gmmR0ChunkMutexAcquire(PGMMR0CHUNKMTXSTATE pMtxState, PGMM pGMM, PGMMCHUNK pChunk, uint32_t fFlags)
{
/*
* Get the lock index and reference the lock.
*/
{
/* Try get an unused one... */
{
{
{
}
}
}
}
/*
* Drop the giant?
*/
if (fFlags != GMMR0CHUNK_MTX_KEEP_GIANT)
{
/** @todo GMM life cycle cleanup (we may race someone
* destroying and cleaning up GMM)? */
}
/*
* Take the chunk mutex.
*/
return rc;
}
/**
* Releases the GMM giant lock.
*
* @returns Assert status code from RTSemFastMutexRequest.
* @param pGMM Pointer to the GMM instance.
* @param pChunk Pointer to the chunk if it's still
* alive, NULL if it isn't. This is used to deassociate
* the chunk from the mutex on the way out so a new one
* can be selected next time, thus avoiding contented
* mutexes.
*/
{
/*
* Release the chunk mutex and reacquire the giant if requested.
*/
else
Assert((pMtxState->fFlags != GMMR0CHUNK_MTX_DROP_GIANT) == (pGMM->hMtxOwner == RTThreadNativeSelf()));
/*
* Drop the chunk mutex user reference and deassociate it from the chunk
* when possible.
*/
&& pChunk
&& RT_SUCCESS(rc) )
{
else
{
if (RT_SUCCESS(rc))
{
}
}
}
return rc;
}
/**
* Drops the giant GMM lock we kept in gmmR0ChunkMutexAcquire while keeping the
* chunk locked.
*
* This only works if gmmR0ChunkMutexAcquire was called with
* GMMR0CHUNK_MTX_KEEP_GIANT. gmmR0ChunkMutexRelease will retake the giant
* mutex, i.e. behave as if GMMR0CHUNK_MTX_RETAKE_GIANT was used.
*
* @returns VBox status code (assuming success is ok).
* @param pMtxState Pointer to the chunk mutex state.
*/
{
/** @todo GMM life cycle cleanup (we may race someone
* destroying and cleaning up GMM)? */
}
/**
* For experimenting with NUMA affinity and such.
*
* @returns The current NUMA Node ID.
*/
static uint16_t gmmR0GetCurrentNumaNodeId(void)
{
#if 1
return GMM_CHUNK_NUMA_ID_UNKNOWN;
#else
return RTMpCpuId() / 16;
#endif
}
/**
* Cleans up when a VM is terminating.
*
* @param pGVM Pointer to the Global VM structure.
*/
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Clean up all registered shared modules first.
*/
#endif
/*
* The policy is 'INVALID' until the initial reservation
* request has been serviced.
*/
{
/*
* If it's the last VM around, we can skip walking all the chunk looking
* for the pages owned by this VM and instead flush the whole shebang.
*
* This takes care of the eventuality that a VM has left shared page
* references behind (shouldn't happen of course, but you never know).
*/
pGMM->cRegisteredVMs--;
/*
* Walk the entire pool looking for pages that belong to this VM
* and leftover mappings. (This'll only catch private pages,
* shared pages will be 'left behind'.)
*/
unsigned iCountDown = 64;
bool fRedoFromStart;
do
{
fRedoFromStart = false;
{
{
/* We left the giant mutex, so reset the yield counters. */
iCountDown = 64;
}
else
{
/* Didn't leave it, so do normal yielding. */
if (!iCountDown)
else
iCountDown--;
}
break;
}
} while (fRedoFromStart);
SUPR0Printf("GMMR0CleanupVM: hGVM=%#x has %#x private pages that cannot be found!\n", pGVM->hSelf, pGVM->gmm.s.cPrivatePages);
/*
* Free empty chunks.
*/
do
{
fRedoFromStart = false;
iCountDown = 10240;
while (pChunk)
{
if ( !pGMM->fBoundMemoryMode
{
{
/* We've left the giant mutex, restart? (+1 for our unlink) */
if (fRedoFromStart)
break;
iCountDown = 10240;
}
}
/* Advance and maybe yield the lock. */
if (--iCountDown == 0)
{
if (fRedoFromStart)
break;
iCountDown = 10240;
}
}
} while (fRedoFromStart);
/*
* Account for shared pages that weren't freed.
*/
{
SUPR0Printf("GMMR0CleanupVM: hGVM=%#x left %#x shared pages behind!\n", pGVM->hSelf, pGVM->gmm.s.cSharedPages);
}
/*
* Clean up balloon statistics in case the VM process crashed.
*/
/*
* Update the over-commitment management statistics.
*/
{
case GMMOCPOLICY_NO_OC:
break;
default:
/** @todo Update GMM->cOverCommittedPages */
break;
}
}
/* zap the GVM data. */
LogFlow(("GMMR0CleanupVM: returns\n"));
}
/**
* Scan one chunk for private pages belonging to the specified VM.
*
* @note This function may drop the gian mutex!
*
* @returns @c true if we've temporarily dropped the giant mutex, @c false if
* we didn't.
* @param pGMM Pointer to the GMM instance.
* @param pGVM The global VM handle.
* @param pChunk The chunk to scan.
*/
{
/*
* Look for pages belonging to the VM.
* (Perform some internal checks while we're scanning.)
*/
#ifndef VBOX_STRICT
#endif
{
unsigned cPrivate = 0;
unsigned cShared = 0;
unsigned cFree = 0;
while (iPage-- > 0)
{
{
/*
* Free the page.
*
* The reason for not using gmmR0FreePrivatePage here is that we
* must *not* cause the chunk to be freed from under us - we're in
* an AVL tree walk here.
*/
cFree++;
}
else
cPrivate++;
}
cFree++;
else
cShared++;
/*
* Did it add up?
*/
{
SUPR0Printf("gmmR0CleanupVMScanChunk: Chunk %p/%#x has bogus stats - free=%d/%d private=%d/%d shared=%d/%d\n",
}
}
/*
* If not in bound memory mode, we should reset the hGVM field
* if it has our handle in it.
*/
{
if (!g_pGMM->fBoundMemoryMode)
{
SUPR0Printf("gmmR0CleanupVMScanChunk: %p/%#x: cFree=%#x - it should be 0 in bound mode!\n",
AssertMsgFailed(("%p/%#x: cFree=%#x - it should be 0 in bound mode!\n", pChunk, pChunk->Core.Key, pChunk->cFree));
}
}
/*
* Look for a mapping belonging to the terminating VM.
*/
for (unsigned i = 0; i < cMappings; i++)
{
cMappings--;
if (i < cMappings)
if (RT_FAILURE(rc))
{
SUPR0Printf("gmmR0CleanupVMScanChunk: %p/%#x: mapping #%x: RTRMemObjFree(%p,false) -> %d \n",
}
return true;
}
return false;
}
/**
* The initial resource reservations.
*
* This will make memory reservations according to policy and priority. If there aren't
* sufficient resources available to sustain the VM this function will fail and all
* future allocations requests will fail as well.
*
* These are just the initial reservations made very very early during the VM creation
* process and will be adjusted later in the GMMR0UpdateReservation call after the
* ring-3 init has completed.
*
* @returns VBox status code.
* @retval VERR_GMM_MEMORY_RESERVATION_DECLINED
* @retval VERR_GMM_
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cBasePages The number of pages that may be allocated for the base RAM and ROMs.
* This does not include MMIO2 and similar.
* @param cShadowPages The number of pages that may be allocated for shadow paging structures.
* @param cFixedPages The number of pages that may be allocated for fixed objects like the
* hyper heap, MMIO2 and similar.
* @param enmPolicy The OC policy to use on this VM.
* @param enmPriority The priority in an out-of-memory situation.
*
* @thread The creator thread / EMT.
*/
GMMR0DECL(int) GMMR0InitialReservation(PVM pVM, VMCPUID idCpu, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages,
{
LogFlow(("GMMR0InitialReservation: pVM=%p cBasePages=%#llx cShadowPages=%#x cFixedPages=%#x enmPolicy=%d enmPriority=%d\n",
/*
* Validate, get basics and take the semaphore.
*/
if (RT_FAILURE(rc))
return rc;
AssertReturn(enmPolicy > GMMOCPOLICY_INVALID && enmPolicy < GMMOCPOLICY_END, VERR_INVALID_PARAMETER);
AssertReturn(enmPriority > GMMPRIORITY_INVALID && enmPriority < GMMPRIORITY_END, VERR_INVALID_PARAMETER);
{
{
/*
* Check if we can accommodate this.
*/
/* ... later ... */
if (RT_SUCCESS(rc))
{
/*
* Update the records.
*/
pGMM->cRegisteredVMs++;
}
}
else
}
else
return rc;
}
/**
* VMMR0 request wrapper for GMMR0InitialReservation.
*
* @returns see GMMR0InitialReservation.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
{
/*
* Validate input and pass it on.
*/
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0InitialReservation(pVM, idCpu, pReq->cBasePages, pReq->cShadowPages, pReq->cFixedPages, pReq->enmPolicy, pReq->enmPriority);
}
/**
* This updates the memory reservation with the additional MMIO2 and ROM pages.
*
* @returns VBox status code.
* @retval VERR_GMM_MEMORY_RESERVATION_DECLINED
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cBasePages The number of pages that may be allocated for the base RAM and ROMs.
* This does not include MMIO2 and similar.
* @param cShadowPages The number of pages that may be allocated for shadow paging structures.
* @param cFixedPages The number of pages that may be allocated for fixed objects like the
* hyper heap, MMIO2 and similar.
*
* @thread EMT.
*/
GMMR0DECL(int) GMMR0UpdateReservation(PVM pVM, VMCPUID idCpu, uint64_t cBasePages, uint32_t cShadowPages, uint32_t cFixedPages)
{
LogFlow(("GMMR0UpdateReservation: pVM=%p cBasePages=%#llx cShadowPages=%#x cFixedPages=%#x\n",
/*
* Validate, get basics and take the semaphore.
*/
if (RT_FAILURE(rc))
return rc;
{
{
/*
* Check if we can accommodate this.
*/
/* ... later ... */
if (RT_SUCCESS(rc))
{
/*
* Update the records.
*/
}
}
else
}
else
return rc;
}
/**
* VMMR0 request wrapper for GMMR0UpdateReservation.
*
* @returns see GMMR0UpdateReservation.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
{
/*
* Validate input and pass it on.
*/
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
}
/**
* Performs sanity checks on a free set.
*
* @returns Error count.
*
* @param pGMM Pointer to the GMM instance.
* @param pSet Pointer to the set.
* @param pszSetName The set name.
* @param pszFunction The function from which it was called.
* @param uLine The line number.
*/
const char *pszFunction, unsigned uLineNo)
{
/*
* Count the free pages in all the chunks and match it against pSet->cFreePages.
*/
{
{
/** @todo check that the chunk is hash into the right set. */
}
}
{
SUPR0Printf("GMM insanity: found %#x pages in the %s set, expected %#x. (%s, line %u)\n",
cErrors++;
}
return cErrors;
}
/**
* Performs some sanity checks on the GMM while owning lock.
*
* @returns Error count.
*
* @param pGMM Pointer to the GMM instance.
* @param pszFunction The function from which it is called.
* @param uLineNo The line number.
*/
{
/** @todo add more sanity checks. */
return cErrors;
}
/**
* Looks up a chunk in the tree and fill in the TLB entry for it.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the allocation chunk, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idChunk The ID of the chunk to find.
* @param pTlbe Pointer to the TLB entry.
*/
{
return pChunk;
}
/**
* Finds a allocation chunk.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the allocation chunk, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idChunk The ID of the chunk to find.
*/
{
/*
* Do a TLB lookup, branch if not in the TLB.
*/
}
/**
* Finds a page.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the page, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idPage The ID of the page to find.
*/
{
return NULL;
}
/**
* Gets the host physical address for a page given by it's ID.
*
* @returns The host physical address or NIL_RTHCPHYS.
* @param pGMM Pointer to the GMM instance.
* @param idPage The ID of the page to find.
*/
{
return NIL_RTHCPHYS;
}
/**
* Selects the appropriate free list given the number of free pages.
*
* @returns Free list index.
* @param cFree The number of free pages in the chunk.
*/
{
AssertMsg(iList < RT_SIZEOFMEMB(GMMCHUNKFREESET, apLists) / RT_SIZEOFMEMB(GMMCHUNKFREESET, apLists[0]),
return iList;
}
/**
* Unlinks the chunk from the free list it's currently on (if any).
*
* @param pChunk The allocation chunk.
*/
{
{
pSet->idGeneration++;
if (pPrev)
else
if (pNext)
}
else
{
}
}
/**
* Links the chunk onto the appropriate free list in the specified free set.
*
* If no free entries, it's not linked into any list.
*
* @param pChunk The allocation chunk.
* @param pSet The free set.
*/
{
{
pSet->idGeneration++;
}
}
/**
* Links the chunk onto the appropriate free list in the specified free set.
*
* If no free entries, it's not linked into any list.
*
* @param pChunk The allocation chunk.
*/
{
if (pGMM->fBoundMemoryMode)
else
}
/**
* Frees a Chunk ID.
*
* @param pGMM Pointer to the GMM instance.
* @param idChunk The Chunk ID to free.
*/
{
}
/**
* Allocates a new Chunk ID.
*
* @returns The Chunk ID.
* @param pGMM Pointer to the GMM instance.
*/
{
AssertCompile(NIL_GMM_CHUNKID == 0);
/*
* Try the next sequential one.
*/
#if 0 /** @todo enable this code */
if ( idChunk <= GMM_CHUNKID_LAST
&& idChunk > NIL_GMM_CHUNKID
return idChunk;
#endif
/*
* Scan sequentially from the last one.
*/
&& idChunk > NIL_GMM_CHUNKID)
{
if (idChunk > NIL_GMM_CHUNKID)
{
AssertMsgReturn(!ASMAtomicBitTestAndSet(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk), NIL_GMM_CHUNKID);
}
}
/*
* Ok, scan from the start.
* We're not racing anyone, so there is no need to expect failures or have restart loops.
*/
AssertMsgReturn(!ASMAtomicBitTestAndSet(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk), NIL_GMM_CHUNKID);
}
/**
* Allocates one private page.
*
* Worker for gmmR0AllocatePages.
*
* @param pGMM Pointer to the GMM instance data.
* @param hGVM The GVM handle of the VM requesting memory.
* @param pChunk The chunk to allocate it from.
* @param pPageDesc The page descriptor.
*/
{
/* update the chunk stats. */
/* unlink the first free page. */
Log3(("A pPage=%p iPage=%#x/%#x u2State=%d iFreeHead=%#x iNext=%#x\n",
/* make the page private. */
pPage->u = 0;
else
/* update the page descriptor. */
}
/**
* Picks the free pages from a chunk.
*
* @returns The new page descriptor table index.
* @param pGMM Pointer to the GMM instance data.
* @param hGVM The VM handle.
* @param pChunk The chunk.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
static uint32_t gmmR0AllocatePagesFromChunk(PGMM pGMM, uint16_t const hGVM, PGMMCHUNK pChunk, uint32_t iPage, uint32_t cPages,
{
return iPage;
}
/**
* Registers a new chunk of memory.
*
* This is called by both gmmR0AllocateOneChunk and GMMR0SeedChunk.
*
* @returns VBox status code. On success, the giant GMM lock will be held, the
* caller must release it (ugly).
* @param pGMM Pointer to the GMM instance.
* @param pSet Pointer to the set.
* @param MemObj The memory object for the chunk.
* @param hGVM The affinity of the chunk. NIL_GVM_HANDLE for no
* affinity.
* @param fChunkFlags The chunk flags, GMM_CHUNK_FLAGS_XXX.
* @param ppChunk Chunk address (out). Optional.
*
* @remarks The caller must not own the giant GMM mutex.
* The giant GMM mutex will be acquired and returned acquired in
* the success path. On failure, no locks will be held.
*/
static int gmmR0RegisterChunk(PGMM pGMM, PGMMCHUNKFREESET pSet, RTR0MEMOBJ MemObj, uint16_t hGVM, uint16_t fChunkFlags,
{
int rc;
if (pChunk)
{
/*
* Initialize it.
*/
/*pChunk->iFreeHead = 0;*/
{
}
/*
* Allocate a Chunk ID and insert it into the tree.
* This has to be done behind the mutex of course.
*/
if (RT_SUCCESS(rc))
{
{
{
LogFlow(("gmmR0RegisterChunk: pChunk=%p id=%#x cChunks=%d\n", pChunk, pChunk->Core.Key, pGMM->cChunks));
if (ppChunk)
return VINF_SUCCESS;
}
/* bail out */
}
else
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Allocate a new chunk, immediately pick the requested pages from it, and adds
* what's remaining to the specified free set.
*
* @note This will leave the giant mutex while allocating the new chunk!
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the kernel-only VM instace data.
* @param pSet Pointer to the free set.
* @param cPages The number of pages requested.
* @param paPages The page descriptor table (input + output).
* @param piPage The pointer to the page descriptor table index
* variable. This will be updated.
*/
{
if (RT_SUCCESS(rc))
{
/** @todo Duplicate gmmR0RegisterChunk here so we can avoid chaining up the
* free pages first and then unchaining them right afterwards. Instead
* do as much work as possible without holding the giant lock. */
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
/* bail out */
}
return rc;
}
/**
* As a last restort we'll pick any page we can get.
*
* @returns The new page descriptor table index.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the global VM structure.
* @param pSet The set to pick from.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
{
while (iList-- > 0)
{
while (pChunk)
{
return iPage;
}
}
return iPage;
}
/**
* Pick pages from empty chunks on the same NUMA node.
*
* @returns The new page descriptor table index.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the global VM structure.
* @param pSet The set to pick from.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
static uint32_t gmmR0AllocatePagesFromEmptyChunksOnSameNode(PGMM pGMM, PGVM pGVM, PGMMCHUNKFREESET pSet,
{
if (pChunk)
{
while (pChunk)
{
{
{
return iPage;
}
}
}
}
return iPage;
}
/**
* Pick pages from non-empty chunks on the same NUMA node.
*
* @returns The new page descriptor table index.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the global VM structure.
* @param pSet The set to pick from.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
{
/** @todo start by picking from chunks with about the right size first? */
unsigned iList = GMM_CHUNK_FREE_SET_UNUSED_LIST;
while (iList-- > 0)
{
while (pChunk)
{
{
{
return iPage;
}
}
}
}
return iPage;
}
/**
* Pick pages that are in chunks already associated with the VM.
*
* @returns The new page descriptor table index.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the global VM structure.
* @param pSet The set to pick from.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
{
/* Hint. */
{
{
return iPage;
}
}
/* Scan. */
{
while (pChunk)
{
{
{
return iPage;
}
}
}
}
return iPage;
}
/**
* Pick pages in bound memory mode.
*
* @returns The new page descriptor table index.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the global VM structure.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
static uint32_t gmmR0AllocatePagesInBoundMode(PGMM pGMM, PGVM pGVM, uint32_t iPage, uint32_t cPages, PGMMPAGEDESC paPages)
{
{
while (pChunk)
{
return iPage;
}
}
return iPage;
}
/**
* Checks if we should start picking pages from chunks of other VMs.
*
* @returns @c true if we should, @c false if we should first try allocate more
* chunks.
*/
{
/*
* Don't allocate a new chunk if we're
*/
/** @todo what about shared pages? */;
return true;
/** @todo make the threshold configurable, also test the code to see if
* this ever kicks in (we might be reserving too much or smth). */
/*
* Check how close we're to the max memory limit and how many fragments
* there are?...
*/
/** @todo. */
return false;
}
/**
* Common worker for GMMR0AllocateHandyPages and GMMR0AllocatePages.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk or
* gmmR0AllocateMoreChunks is necessary.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
*
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the shared VM structure.
* @param cPages The number of pages to allocate.
* @param paPages Pointer to the page descriptors.
* See GMMPAGEDESC for details on what is expected on input.
* @param enmAccount The account to charge.
*
* @remarks Call takes the giant GMM lock.
*/
static int gmmR0AllocatePagesNew(PGMM pGMM, PGVM pGVM, uint32_t cPages, PGMMPAGEDESC paPages, GMMACCOUNT enmAccount)
{
/*
* Check allocation limits.
*/
return VERR_GMM_HIT_GLOBAL_LIMIT;
switch (enmAccount)
{
case GMMACCOUNT_BASE:
{
Log(("gmmR0AllocatePages:Base: Reserved=%#llx Allocated+Ballooned+Requested=%#llx+%#llx+%#x!\n",
pGVM->gmm.s.Reserved.cBasePages, pGVM->gmm.s.Allocated.cBasePages, pGVM->gmm.s.cBalloonedPages, cPages));
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
break;
case GMMACCOUNT_SHADOW:
{
Log(("gmmR0AllocatePages:Shadow: Reserved=%#x Allocated+Requested=%#x+%#x!\n",
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
break;
case GMMACCOUNT_FIXED:
{
Log(("gmmR0AllocatePages:Fixed: Reserved=%#x Allocated+Requested=%#x+%#x!\n",
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
break;
default:
}
/*
* If we're in legacy memory mode, it's easy to figure if we have
* sufficient number of pages up-front.
*/
if ( pGMM->fLegacyAllocationMode
{
return VERR_GMM_SEED_ME;
}
/*
* Update the accounts before we proceed because we might be leaving the
* protection of the global mutex and thus run the risk of permitting
* too much memory to be allocated.
*/
switch (enmAccount)
{
}
/*
* Part two of it's-easy-in-legacy-memory-mode.
*/
if (pGMM->fLegacyAllocationMode)
{
return VINF_SUCCESS;
}
/*
* Bound mode is also relatively straightforward.
*/
int rc = VINF_SUCCESS;
if (pGMM->fBoundMemoryMode)
{
do
}
/*
* Shared mode is trickier as we should try archive the same locality as
* in bound mode, but smartly make use of non-full chunks allocated by
* other VMs if we're low on memory.
*/
else
{
/* Pick the most optimal pages first. */
{
/* Maybe we should try getting pages from chunks "belonging" to
other VMs before allocating more chunks? */
/* Allocate memory from empty chunks. */
iPage = gmmR0AllocatePagesFromEmptyChunksOnSameNode(pGMM, pGVM, &pGMM->PrivateX, iPage, cPages, paPages);
/* Grab empty shared chunks. */
iPage = gmmR0AllocatePagesFromEmptyChunksOnSameNode(pGMM, pGVM, &pGMM->Shared, iPage, cPages, paPages);
/*
* Ok, try allocate new chunks.
*/
{
do
/* If the host is out of memory, take whatever we can get. */
if ( rc == VERR_NO_MEMORY
{
rc = VINF_SUCCESS;
}
}
}
}
/*
* Clean up on failure. Since this is bound to be a low-memory condition
* we will give back any empty chunks that might be hanging around.
*/
if (RT_FAILURE(rc))
{
/* Update the statistics. */
switch (enmAccount)
{
}
/* Release the pages. */
while (iPage-- > 0)
{
{
}
else
}
/* Free empty chunks. */
/** @todo */
}
return VINF_SUCCESS;
}
/**
* Updates the previous allocations and allocates more pages.
*
* The handy pages are always taken from the 'base' memory account.
* The allocated pages are not cleared and will contains random garbage.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_OWNER if the caller is not an EMT.
* @retval VERR_GMM_PAGE_NOT_FOUND if one of the pages to update wasn't found.
* @retval VERR_GMM_PAGE_NOT_PRIVATE if one of the pages to update wasn't a
* private page.
* @retval VERR_GMM_PAGE_NOT_SHARED if one of the pages to update wasn't a
* shared page.
* @retval VERR_GMM_NOT_PAGE_OWNER if one of the pages to be updated wasn't
* owned by the VM.
* @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk is necessary.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cPagesToUpdate The number of pages to update (starting from the head).
* @param cPagesToAlloc The number of pages to allocate (starting from the head).
* @param paPages The array of page descriptors.
* See GMMPAGEDESC for details on what is expected on input.
* @thread EMT.
*/
GMMR0DECL(int) GMMR0AllocateHandyPages(PVM pVM, VMCPUID idCpu, uint32_t cPagesToUpdate, uint32_t cPagesToAlloc, PGMMPAGEDESC paPages)
{
LogFlow(("GMMR0AllocateHandyPages: pVM=%p cPagesToUpdate=%#x cPagesToAlloc=%#x paPages=%p\n",
/*
* Validate, get basics and take the semaphore.
* (This is a relatively busy path, so make predictions where possible.)
*/
if (RT_FAILURE(rc))
return rc;
unsigned iPage = 0;
{
/*|| paPages[iPage].idPage == NIL_GMM_PAGEID*/,
/*|| paPages[iPage].idSharedPage == NIL_GMM_PAGEID*/,
}
{
AssertMsgReturn(paPages[iPage].HCPhysGCPhys == NIL_RTHCPHYS, ("#%#x: %RHp\n", iPage, paPages[iPage].HCPhysGCPhys), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idSharedPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
}
{
/* No allocations before the initial reservation has been made! */
{
/*
* Perform the updates.
* Stop on the first error.
*/
{
{
{
{
{
/* else: NIL_RTHCPHYS nothing */
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not owner! hGVM=%#x hSelf=%#x\n",
break;
}
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not private! %.*Rhxs (type %d)\n", iPage, paPages[iPage].idPage, sizeof(*pPage), pPage, pPage->Common.u2State));
break;
}
}
else
{
break;
}
}
{
{
{
Log(("GMMR0AllocateHandyPages: free shared page %x cRefs=%d\n", paPages[iPage].idSharedPage, pPage->Shared.cRefs));
else
{
pGMM->cDuplicatePages--;
}
}
else
{
break;
}
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not found! (shared)\n", iPage, paPages[iPage].idSharedPage));
break;
}
}
} /* for each page to update */
if (RT_SUCCESS(rc))
{
#if defined(VBOX_STRICT) && 0 /** @todo re-test this later. Appeared to be a PGM init bug. */
{
}
#endif
/*
* Join paths with GMMR0AllocatePages for the allocation.
* Note! gmmR0AllocateMoreChunks may leave the protection of the mutex!
*/
}
}
else
}
else
return rc;
}
/**
* Allocate one or more pages.
*
* This is typically used for ROMs and MMIO2 (VRAM) during VM creation.
* The allocated pages are not cleared and will contains random garbage.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_OWNER if the caller is not an EMT.
* @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk is necessary.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cPages The number of pages to allocate.
* @param paPages Pointer to the page descriptors.
* See GMMPAGEDESC for details on what is expected on input.
* @param enmAccount The account to charge.
*
* @thread EMT.
*/
GMMR0DECL(int) GMMR0AllocatePages(PVM pVM, VMCPUID idCpu, uint32_t cPages, PGMMPAGEDESC paPages, GMMACCOUNT enmAccount)
{
LogFlow(("GMMR0AllocatePages: pVM=%p cPages=%#x paPages=%p enmAccount=%d\n", pVM, cPages, paPages, enmAccount));
/*
* Validate, get basics and take the semaphore.
*/
if (RT_FAILURE(rc))
return rc;
AssertMsgReturn(enmAccount > GMMACCOUNT_INVALID && enmAccount < GMMACCOUNT_END, ("%d\n", enmAccount), VERR_INVALID_PARAMETER);
AssertMsgReturn(cPages > 0 && cPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cPages), VERR_INVALID_PARAMETER);
{
|| ( enmAccount == GMMACCOUNT_BASE
AssertMsgReturn(paPages[iPage].idPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idSharedPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
}
{
/* No allocations before the initial reservation has been made! */
else
}
else
return rc;
}
/**
* VMMR0 request wrapper for GMMR0AllocatePages.
*
* @returns see GMMR0AllocatePages.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
{
/*
* Validate input and pass it on.
*/
}
/**
* Allocate a large page to represent guest RAM
*
* The allocated pages are not cleared and will contains random garbage.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_OWNER if the caller is not an EMT.
* @retval VERR_GMM_SEED_ME if seeding via GMMR0SeedChunk is necessary.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
* @returns see GMMR0AllocatePages.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cbPage Large page size
*/
GMMR0DECL(int) GMMR0AllocateLargePage(PVM pVM, VMCPUID idCpu, uint32_t cbPage, uint32_t *pIdPage, RTHCPHYS *pHCPhys)
{
/*
* Validate, get basics and take the semaphore.
*/
if (RT_FAILURE(rc))
return rc;
/* Not supported in legacy mode where we allocate the memory in ring 3 and lock it in ring 0. */
if (pGMM->fLegacyAllocationMode)
return VERR_NOT_SUPPORTED;
*pHCPhys = NIL_RTHCPHYS;
{
{
Log(("GMMR0AllocateLargePage: Reserved=%#llx Allocated+Requested=%#llx+%#x!\n",
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
/*
* Allocate a new large page chunk.
*
* Note! We leave the giant GMM lock temporarily as the allocation might
* take a long time. gmmR0RegisterChunk will retake it (ugly).
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Allocate all the pages in the chunk.
*/
/* Unlink the new chunk from the free list. */
/** @todo rewrite this to skip the looping. */
/* Allocate all pages. */
/* Return the first page as we'll use the whole chunk as one big page. */
for (unsigned i = 1; i < cPages; i++)
/* Update accounting. */
}
else
}
}
else
{
}
return rc;
}
/**
* Free a large page
*
* @returns VBox status code:
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param idPage Large page id
*/
{
/*
* Validate, get basics and take the semaphore.
*/
if (RT_FAILURE(rc))
return rc;
/* Not supported in legacy mode where we allocate the memory in ring 3 and lock it in ring 0. */
if (pGMM->fLegacyAllocationMode)
return VERR_NOT_SUPPORTED;
{
{
Log(("GMMR0FreeLargePage: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Allocated.cBasePages, cPages));
}
&& GMM_PAGE_IS_PRIVATE(pPage)))
{
/* Release the memory immediately. */
/* Update accounting. */
}
else
}
else
return rc;
}
/**
* VMMR0 request wrapper for GMMR0FreeLargePage.
*
* @returns see GMMR0FreeLargePage.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
{
/*
* Validate input and pass it on.
*/
}
/**
* Frees a chunk, giving it back to the host OS.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM This is set when called from GMMR0CleanupVM so we can
* unmap and free the chunk in one go.
* @param pChunk The chunk to free.
* @param fRelaxedSem Whether we can release the semaphore while doing the
* freeing (@c true) or not.
*/
{
/*
* Cleanup hack! Unmap the chunk from the callers address space.
* This shouldn't happen, so screw lock contention...
*/
if ( pChunk->cMappingsX
&& pGVM)
/*
* If there are current mappings of the chunk, then request the
* VMs to unmap them. Reposition the chunk in the free list so
* it won't be a likely candidate for allocations.
*/
if (pChunk->cMappingsX)
{
/** @todo R0 -> VM request */
/* The chunk can be mapped by more than one VM if fBoundMemoryMode is false! */
return false;
}
/*
* Save and trash the handle.
*/
/*
* Unlink it from everywhere.
*/
{
}
/*
* Free the Chunk ID before dropping the locks and freeing the rest.
*/
pGMM->cFreedChunks++;
if (fRelaxedSem)
if (fRelaxedSem)
return fRelaxedSem;
}
/**
* Free page worker.
*
* The caller does all the statistic decrementing, we do all the incrementing.
*
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the GVM instance.
* @param pChunk Pointer to the chunk this page belongs to.
* @param idPage The Page ID.
* @param pPage Pointer to the page.
*/
static void gmmR0FreePageWorker(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, uint32_t idPage, PGMMPAGE pPage)
{
Log3(("F pPage=%p iPage=%#x/%#x u2State=%d iFreeHead=%#x\n",
pPage, pPage - &pChunk->aPages[0], idPage, pPage->Common.u2State, pChunk->iFreeHead)); NOREF(idPage);
/*
* Put the page on the free list.
*/
pPage->u = 0;
/*
* and relink the chunk if necessary.
*/
if ( !cFree
{
}
else
{
}
/*
* If the chunk becomes empty, consider giving memory back to the host OS.
*
* The current strategy is to try give it back if there are other chunks
* in this free list, meaning if there are at least 240 free pages in this
* category. Note that since there are probably mappings of the chunk,
* it won't be freed up instantly, which probably screws up this logic
* a bit...
*/
/** @todo Do this on the way out. */
&& !pGMM->fLegacyAllocationMode))
}
/**
* Frees a shared page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param idPage The Page ID
* @param pPage The page structure.
*/
{
pGMM->cAllocatedPages--;
pGMM->cSharedPages--;
}
/**
* Frees a private page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param idPage The Page ID
* @param pPage The page structure.
*/
{
pGMM->cAllocatedPages--;
}
/**
* Common worker for GMMR0FreePages and GMMR0BalloonedPages.
*
* @returns VBox status code:
* @retval xxx
*
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the shared VM structure.
* @param cPages The number of pages to free.
* @param paPages Pointer to the page descriptors.
* @param enmAccount The account this relates to.
*/
static int gmmR0FreePages(PGMM pGMM, PGVM pGVM, uint32_t cPages, PGMMFREEPAGEDESC paPages, GMMACCOUNT enmAccount)
{
/*
* Check that the request isn't impossible wrt to the account status.
*/
switch (enmAccount)
{
case GMMACCOUNT_BASE:
{
}
break;
case GMMACCOUNT_SHADOW:
{
}
break;
case GMMACCOUNT_FIXED:
{
}
break;
default:
}
/*
* Walk the descriptors and free the pages.
*
* Statistics (except the account) are being updated as we go along,
* unlike the alloc code. Also, stop on the first error.
*/
int rc = VINF_SUCCESS;
{
{
{
{
}
else
{
break;
}
}
{
else
{
pGMM->cDuplicatePages--;
}
}
else
{
break;
}
}
else
{
break;
}
}
/*
* Update the account.
*/
switch (enmAccount)
{
default:
}
/*
* Any threshold stuff to be done here?
*/
return rc;
}
/**
* Free one or more pages.
*
* This is typically used at reset time or power off.
*
* @returns VBox status code:
* @retval xxx
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cPages The number of pages to allocate.
* @param paPages Pointer to the page descriptors containing the Page IDs for each page.
* @param enmAccount The account this relates to.
* @thread EMT.
*/
GMMR0DECL(int) GMMR0FreePages(PVM pVM, VMCPUID idCpu, uint32_t cPages, PGMMFREEPAGEDESC paPages, GMMACCOUNT enmAccount)
{
LogFlow(("GMMR0FreePages: pVM=%p cPages=%#x paPages=%p enmAccount=%d\n", pVM, cPages, paPages, enmAccount));
/*
* Validate input and get the basics.
*/
if (RT_FAILURE(rc))
return rc;
AssertMsgReturn(enmAccount > GMMACCOUNT_INVALID && enmAccount < GMMACCOUNT_END, ("%d\n", enmAccount), VERR_INVALID_PARAMETER);
AssertMsgReturn(cPages > 0 && cPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cPages), VERR_INVALID_PARAMETER);
/*|| paPages[iPage].idPage == NIL_GMM_PAGEID*/,
/*
* Take the semaphore and call the worker function.
*/
{
}
else
return rc;
}
/**
* VMMR0 request wrapper for GMMR0FreePages.
*
* @returns see GMMR0FreePages.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
{
/*
* Validate input and pass it on.
*/
}
/**
* Report back on a memory ballooning request.
*
* The request may or may not have been initiated by the GMM. If it was initiated
* by the GMM it is important that this function is called even if no pages were
* ballooned.
*
* @returns VBox status code:
* @retval VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH
* @retval VERR_GMM_ATTEMPT_TO_DEFLATE_TOO_MUCH
* @retval VERR_GMM_OVERCOMMITTED_TRY_AGAIN_IN_A_BIT - reset condition
* indicating that we won't necessarily have sufficient RAM to boot
* the VM again and that it should pause until this changes (we'll try
* balloon some other VM). (For standard deflate we have little choice
* but to hope the VM won't use the memory that was returned to it.)
*
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param cBalloonedPages The number of pages that was ballooned.
*
* @thread EMT.
*/
GMMR0DECL(int) GMMR0BalloonedPages(PVM pVM, VMCPUID idCpu, GMMBALLOONACTION enmAction, uint32_t cBalloonedPages)
{
LogFlow(("GMMR0BalloonedPages: pVM=%p enmAction=%d cBalloonedPages=%#x\n",
AssertMsgReturn(cBalloonedPages < RT_BIT(32 - PAGE_SHIFT), ("%#x\n", cBalloonedPages), VERR_INVALID_PARAMETER);
/*
* Validate input and get the basics.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
{
switch (enmAction)
{
case GMMBALLOONACTION_INFLATE:
{
if (RT_LIKELY(pGVM->gmm.s.Allocated.cBasePages + pGVM->gmm.s.cBalloonedPages + cBalloonedPages <= pGVM->gmm.s.Reserved.cBasePages))
{
/*
* Record the ballooned memory.
*/
{
/* Codepath never taken. Might be interesting in the future to request ballooned memory from guests in low memory conditions.. */
AssertFailed();
Log(("GMMR0BalloonedPages: +%#x - Global=%#llx / VM: Total=%#llx Req=%#llx Actual=%#llx (pending)\n", cBalloonedPages,
pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages, pGVM->gmm.s.cReqBalloonedPages, pGVM->gmm.s.cReqActuallyBalloonedPages));
}
else
{
Log(("GMMR0BalloonedPages: +%#x - Global=%#llx / VM: Total=%#llx (user)\n",
}
}
else
{
Log(("GMMR0BalloonedPages: cBasePages=%#llx Total=%#llx cBalloonedPages=%#llx Reserved=%#llx\n",
pGVM->gmm.s.Allocated.cBasePages, pGVM->gmm.s.cBalloonedPages, cBalloonedPages, pGVM->gmm.s.Reserved.cBasePages));
}
break;
}
case GMMBALLOONACTION_DEFLATE:
{
/* Deflate. */
{
/*
* Record the ballooned memory.
*/
{
AssertFailed(); /* This is path is for later. */
Log(("GMMR0BalloonedPages: -%#x - Global=%#llx / VM: Total=%#llx Req=%#llx\n",
cBalloonedPages, pGMM->cBalloonedPages, pGVM->gmm.s.cBalloonedPages, pGVM->gmm.s.cReqDeflatePages));
/*
* Anything we need to do here now when the request has been completed?
*/
}
else
Log(("GMMR0BalloonedPages: -%#x - Global=%#llx / VM: Total=%#llx (user)\n",
}
else
{
Log(("GMMR0BalloonedPages: Total=%#llx cBalloonedPages=%#llx\n", pGVM->gmm.s.cBalloonedPages, cBalloonedPages));
}
break;
}
case GMMBALLOONACTION_RESET:
{
/* Reset to an empty balloon. */
break;
}
default:
break;
}
}
else
return rc;
}
/**
* VMMR0 request wrapper for GMMR0BalloonedPages.
*
* @returns see GMMR0BalloonedPages.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
{
/*
* Validate input and pass it on.
*/
}
/**
* Return memory statistics for the hypervisor
*
* @returns VBox status code:
* @param pVM Pointer to the shared VM structure.
* @param pReq The request packet.
*/
{
/*
* Validate input and pass it on.
*/
/*
* Validate input and get the basics.
*/
return VINF_SUCCESS;
}
/**
* Return memory statistics for the VM
*
* @returns VBox status code:
* @param pVM Pointer to the shared VM structure.
* @parma idCpu Cpu id.
* @param pReq The request packet.
*/
{
/*
* Validate input and pass it on.
*/
/*
* Validate input and get the basics.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
{
}
else
return rc;
}
/**
* Worker for gmmR0UnmapChunk and gmmr0FreeChunk.
*
* Don't call this in legacy allocation mode!
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be unmapped.
*/
{
/*
* Find the mapping and try unmapping it.
*/
{
{
/* unmap */
if (RT_SUCCESS(rc))
{
/* update the record. */
cMappings--;
if (i < cMappings)
}
return rc;
}
}
Log(("gmmR0UnmapChunk: Chunk %#x is not mapped into pGVM=%p/%#x\n", pChunk->Core.Key, pGVM, pGVM->hSelf));
return VERR_GMM_CHUNK_NOT_MAPPED;
}
/**
* Unmaps a chunk previously mapped into the address space of the current process.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be unmapped.
*/
{
if (!pGMM->fLegacyAllocationMode)
{
/*
* Lock the chunk and if possible leave the giant GMM lock.
*/
if (RT_SUCCESS(rc))
{
}
return rc;
}
return VINF_SUCCESS;
Log(("gmmR0UnmapChunk: Chunk %#x is not mapped into pGVM=%p/%#x (legacy)\n", pChunk->Core.Key, pGVM, pGVM->hSelf));
return VERR_GMM_CHUNK_NOT_MAPPED;
}
/**
* Worker for gmmR0MapChunk.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be mapped.
* @param ppvR3 Where to store the ring-3 address of the mapping.
* In the VERR_GMM_CHUNK_ALREADY_MAPPED case, this will be
* contain the address of the existing mapping.
*/
{
/*
* If we're in legacy mode this is simple.
*/
if (pGMM->fLegacyAllocationMode)
{
{
return VERR_GMM_CHUNK_NOT_FOUND;
}
return VINF_SUCCESS;
}
/*
* Check to see if the chunk is already mapped.
*/
{
{
#ifdef VBOX_WITH_PAGE_SHARING
/* The ring-3 chunk cache can be out of sync; don't fail. */
return VINF_SUCCESS;
#else
return VERR_GMM_CHUNK_ALREADY_MAPPED;
#endif
}
}
/*
* Do the mapping.
*/
int rc = RTR0MemObjMapUser(&hMapObj, pChunk->hMemObj, (RTR3PTR)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
if (RT_SUCCESS(rc))
{
/* reallocate the array? assumes few users per chunk (usually one). */
if ( iMapping <= 3
|| (iMapping & 3) == 0)
{
? iMapping + 1
: iMapping + 4;
{
return VERR_GMM_TOO_MANY_CHUNK_MAPPINGS;
}
if (RT_UNLIKELY(!pvMappings))
{
return VERR_NO_MEMORY;
}
}
/* insert new entry */
}
return rc;
}
/**
* Maps a chunk into the user address space of the current process.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be mapped.
* @param fRelaxedSem Whether we can release the semaphore while doing the
* mapping (@c true) or not.
* @param ppvR3 Where to store the ring-3 address of the mapping.
* In the VERR_GMM_CHUNK_ALREADY_MAPPED case, this will be
* contain the address of the existing mapping.
*/
{
/*
* Take the chunk lock and leave the giant GMM lock when possible, then
* call the worker function.
*/
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Check if a chunk is mapped into the specified VM
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be mapped.
* @param ppvR3 Where to store the ring-3 address of the mapping.
*/
{
{
{
return true;
}
}
return false;
}
/**
*
* The mapping and unmapping applies to the current process.
*
* This API does two things because it saves a kernel call per mapping when
* when the ring-3 mapping cache is full.
*
* @returns VBox status code.
* @param pVM The VM.
* @param idChunkMap The chunk to map. NIL_GMM_CHUNKID if nothing to map.
* @param idChunkUnmap The chunk to unmap. NIL_GMM_CHUNKID if nothing to unmap.
* @param ppvR3 Where to store the address of the mapped chunk. NULL is ok if nothing to map.
* @thread EMT
*/
GMMR0DECL(int) GMMR0MapUnmapChunk(PVM pVM, uint32_t idChunkMap, uint32_t idChunkUnmap, PRTR3PTR ppvR3)
{
LogFlow(("GMMR0MapUnmapChunk: pVM=%p idChunkMap=%#x idChunkUnmap=%#x ppvR3=%p\n",
/*
* Validate input and get the basics.
*/
if (RT_FAILURE(rc))
return rc;
AssertCompile(NIL_GMM_CHUNKID == 0);
if ( idChunkMap == NIL_GMM_CHUNKID
&& idChunkUnmap == NIL_GMM_CHUNKID)
return VERR_INVALID_PARAMETER;
if (idChunkMap != NIL_GMM_CHUNKID)
{
*ppvR3 = NIL_RTR3PTR;
}
/*
* Take the semaphore and do the work.
*
* The unmapping is done last since it's easier to undo a mapping than
* undoing an unmapping. The ring-3 mapping cache cannot not be so big
* that it pushes the user virtual address space to within a chunk of
* it it's limits, so, no problem here.
*/
{
if (idChunkMap != NIL_GVM_HANDLE)
{
else
{
}
}
/** @todo split this operation, the bail out might (theoretcially) not be
* entirely safe. */
if ( idChunkUnmap != NIL_GMM_CHUNKID
&& RT_SUCCESS(rc))
{
else
{
}
}
}
else
return rc;
}
/**
* VMMR0 request wrapper for GMMR0MapUnmapChunk.
*
* @returns see GMMR0MapUnmapChunk.
* @param pVM Pointer to the shared VM structure.
* @param pReq The request packet.
*/
{
/*
* Validate input and pass it on.
*/
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
}
/**
* Legacy mode API for supplying pages.
*
* The specified user address points to a allocation chunk sized block that
* will be locked down and used by the GMM when the GM asks for pages.
*
* @returns VBox status code.
* @param pVM The VM.
* @param idCpu VCPU id
* @param pvR3 Pointer to the chunk size memory block to lock down.
*/
{
/*
* Validate input and get the basics.
*/
if (RT_FAILURE(rc))
return rc;
if (!pGMM->fLegacyAllocationMode)
{
Log(("GMMR0SeedChunk: not in legacy allocation mode!\n"));
return VERR_NOT_SUPPORTED;
}
/*
* Lock the memory and add it as new chunk with our hGVM.
* (The GMM locking is done inside gmmR0RegisterChunk.)
*/
rc = RTR0MemObjLockUser(&MemObj, pvR3, GMM_CHUNK_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
else
}
return rc;
}
typedef struct
{
char *pszModuleName;
char *pszVersion;
/**
* Tree enumeration callback for finding identical modules by name and version
*/
{
if ( pInfo
/** @todo replace with RTStrNCmp */
{
return 1; /* stop search */
}
return 0;
}
/**
* Registers a new shared module for the VM
*
* @returns VBox status code.
* @param pVM VM handle
* @param idCpu VCPU id
* @param enmGuestOS Guest OS type
* @param pszModuleName Module name
* @param pszVersion Module version
* @param GCBaseAddr Module base address
* @param cbModule Module size
* @param cRegions Number of shared region descriptors
* @param pRegions Shared region(s)
*/
GMMR0DECL(int) GMMR0RegisterSharedModule(PVM pVM, VMCPUID idCpu, VBOXOSFAMILY enmGuestOS, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule,
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
if (RT_FAILURE(rc))
return rc;
Log(("GMMR0RegisterSharedModule %s %s base %RGv size %x\n", pszModuleName, pszVersion, GCBaseAddr, cbModule));
/*
* Take the semaphore and do some more validations.
*/
{
bool fNewModule = false;
/* Check if this module is already locally registered. */
PGMMSHAREDMODULEPERVM pRecVM = (PGMMSHAREDMODULEPERVM)RTAvlGCPtrGet(&pGVM->gmm.s.pSharedModuleTree, GCBaseAddr);
if (!pRecVM)
{
if (!pRecVM)
{
AssertFailed();
rc = VERR_NO_MEMORY;
goto end;
}
/* Save the region data as they can differ between VMs (address space scrambling or simply different loading order) */
for (unsigned i = 0; i < cRegions; i++)
{
}
fNewModule = true;
}
else
/* Check if this module is already globally registered. */
PGMMSHAREDMODULE pGlobalModule = (PGMMSHAREDMODULE)RTAvlGCPtrGet(&pGMM->pGlobalSharedModuleTree, GCBaseAddr);
if ( !pGlobalModule
&& enmGuestOS == VBOXOSFAMILY_Windows64)
{
/* Two identical copies of e.g. Win7 x64 will typically not have a similar virtual address space layout for dlls or kernel modules.
* Try to find identical binaries based on name and version.
*/
int ret = RTAvlGCPtrDoWithAll(&pGMM->pGlobalSharedModuleTree, true /* fFromLeft */, gmmR0CheckForIdenticalModule, &Info);
if (ret == 1)
{
}
}
if (!pGlobalModule)
{
if (!pGlobalModule)
{
AssertFailed();
rc = VERR_NO_MEMORY;
goto end;
}
/* Input limit already safe; no need to check again. */
/** @todo replace with RTStrCopy */
for (unsigned i = 0; i < cRegions; i++)
{
}
/* Save reference. */
pRecVM->fCollision = false;
pGlobalModule->cUsers++;
rc = VINF_SUCCESS;
}
else
{
/* Make sure the name and version are identical. */
/** @todo replace with RTStrNCmp */
{
/* Save reference. */
if ( fNewModule
|| pRecVM->fCollision == true) /* colliding module unregistered and new one registered since the last check */
{
pGlobalModule->cUsers++;
Log(("GMMR0RegisterSharedModule: using existing module %s cUser=%d!\n", pszModuleName, pGlobalModule->cUsers));
}
pRecVM->fCollision = false;
rc = VINF_SUCCESS;
}
else
{
pRecVM->fCollision = true;
goto end;
}
}
}
else
end:
return rc;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* VMMR0 request wrapper for GMMR0RegisterSharedModule.
*
* @returns see GMMR0RegisterSharedModule.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0RegisterSharedModuleReq(PVM pVM, VMCPUID idCpu, PGMMREGISTERSHAREDMODULEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertMsgReturn(pReq->Hdr.cbReq >= sizeof(*pReq) && pReq->Hdr.cbReq == RT_UOFFSETOF(GMMREGISTERSHAREDMODULEREQ, aRegions[pReq->cRegions]), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
/* Pass back return code in the request packet to preserve informational codes. (VMMR3CallR0 chokes on them) */
pReq->rc = GMMR0RegisterSharedModule(pVM, idCpu, pReq->enmGuestOS, pReq->szName, pReq->szVersion, pReq->GCBaseAddr, pReq->cbModule, pReq->cRegions, pReq->aRegions);
return VINF_SUCCESS;
}
/**
* Unregisters a shared module for the VM
*
* @returns VBox status code.
* @param pVM VM handle
* @param idCpu VCPU id
* @param pszModuleName Module name
* @param pszVersion Module version
* @param GCBaseAddr Module base address
* @param cbModule Module size
*/
GMMR0DECL(int) GMMR0UnregisterSharedModule(PVM pVM, VMCPUID idCpu, char *pszModuleName, char *pszVersion, RTGCPTR GCBaseAddr, uint32_t cbModule)
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
if (RT_FAILURE(rc))
return rc;
Log(("GMMR0UnregisterSharedModule %s %s base=%RGv size %x\n", pszModuleName, pszVersion, GCBaseAddr, cbModule));
/*
* Take the semaphore and do some more validations.
*/
{
PGMMSHAREDMODULEPERVM pRecVM = (PGMMSHAREDMODULEPERVM)RTAvlGCPtrGet(&pGVM->gmm.s.pSharedModuleTree, GCBaseAddr);
if (pRecVM)
{
/* Remove reference to global shared module. */
if (!pRecVM->fCollision)
{
if (pRec) /* paranoia */
{
{
/* Free the ranges, but leave the pages intact as there might still be references; they will be cleared by the COW mechanism. */
#ifdef VBOX_STRICT
{
}
#endif
/* Remove from the tree and free memory. */
}
}
else
}
else
/* Remove from the tree and free memory. */
}
else
}
else
return rc;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* VMMR0 request wrapper for GMMR0UnregisterSharedModule.
*
* @returns see GMMR0UnregisterSharedModule.
* @param pVM Pointer to the shared VM structure.
* @param idCpu VCPU id
* @param pReq The request packet.
*/
GMMR0DECL(int) GMMR0UnregisterSharedModuleReq(PVM pVM, VMCPUID idCpu, PGMMUNREGISTERSHAREDMODULEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0UnregisterSharedModule(pVM, idCpu, pReq->szName, pReq->szVersion, pReq->GCBaseAddr, pReq->cbModule);
}
#ifdef VBOX_WITH_PAGE_SHARING
/**
* Increase the use count of a shared page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param pPage The page structure.
*/
{
pGMM->cDuplicatePages++;
}
/**
* Converts a private page to a shared page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param HCPhys Host physical address
* @param idPage The Page ID
* @param pPage The page structure.
*/
DECLINLINE(void) gmmR0ConvertToSharedPage(PGMM pGMM, PGVM pGVM, RTHCPHYS HCPhys, uint32_t idPage, PGMMPAGE pPage)
{
pGMM->cSharedPages++;
/* Modify the page structure. */
}
/**
* Checks specified shared module range for changes
*
* Performs the following tasks:
* - If a shared page is new, then it changes the GMM page type to shared and
* returns it in the pPageDesc descriptor.
* - If a shared page already exists, then it checks if the VM page is
* identical and if so frees the VM page and returns the shared page in
* pPageDesc descriptor.
*
* @remarks ASSUMES the caller has acquired the GMM semaphore!!
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the GVM instance data.
* @param pModule Module description
* @param idxRegion Region index
* @param idxPage Page index
* @param paPageDesc Page descriptor
*/
GMMR0DECL(int) GMMR0SharedModuleCheckPage(PGVM pGVM, PGMMSHAREDMODULE pModule, unsigned idxRegion, unsigned idxPage,
{
int rc = VINF_SUCCESS;
LogFlow(("GMMR0SharedModuleCheckRange %s base %RGv region %d idxPage %d\n", pModule->szName, pModule->Core.Key, idxRegion, idxPage));
if (!pGlobalRegion->paHCPhysPageID)
{
/* First time; create a page descriptor array. */
pGlobalRegion->paHCPhysPageID = (uint32_t *)RTMemAlloc(cPages * sizeof(*pGlobalRegion->paHCPhysPageID));
if (!pGlobalRegion->paHCPhysPageID)
{
AssertFailed();
rc = VERR_NO_MEMORY;
goto end;
}
/* Invalidate all descriptors. */
for (unsigned i = 0; i < cPages; i++)
}
/* We've seen this shared page for the first time? */
{
/* Easy case: just change the internal page type. */
if (!pPage)
{
Log(("GMMR0SharedModuleCheckPage: Invalid idPage=%#x #1 (GCPhys=%RGp HCPhys=%RHp idxRegion=%#x idxPage=%#x)\n",
AssertFailed();
goto end;
}
AssertMsg(pPageDesc->GCPhys == (pPage->Private.pfn << 12), ("desc %RGp gmm %RGp\n", pPageDesc->HCPhys, (pPage->Private.pfn << 12)));
/* Keep track of these references. */
}
else
{
Log(("Replace existing page guest %RGp host %RHp id %x -> id %x\n", pPageDesc->GCPhys, pPageDesc->HCPhys, pPageDesc->uHCPhysPageId, pGlobalRegion->paHCPhysPageID[idxPage]));
/* Get the shared page source. */
if (!pPage)
{
Log(("GMMR0SharedModuleCheckPage: Invalid idPage=%#x #2 (idxRegion=%#x idxPage=%#x)\n",
AssertFailed();
goto end;
}
{
/* Page was freed at some point; invalidate this entry. */
/** @todo this isn't really bullet proof. */
Log(("Old shared page was freed -> create a new one\n"));
goto new_shared_page; /* ugly goto */
}
Log(("Replace existing page guest host %RHp -> %RHp\n", pPageDesc->HCPhys, ((uint64_t)pPage->Shared.pfn) << PAGE_SHIFT));
/* Calculate the virtual address of the local page. */
if (pChunk)
{
{
AssertFailed();
goto end;
}
}
else
{
AssertFailed();
goto end;
}
/* Calculate the virtual address of the shared page. */
/* Get the virtual address of the physical page; map the chunk into the VM process if not already done. */
{
Log(("Map chunk into process!\n"));
if (rc != VINF_SUCCESS)
{
goto end;
}
}
pbSharedPage = pbChunk + ((pGlobalRegion->paHCPhysPageID[idxPage] & GMM_PAGEID_IDX_MASK) << PAGE_SHIFT);
/** @todo write ASMMemComparePage. */
{
Log(("Unexpected differences found between local and shared page; skip\n"));
/* Signal to the caller that this one hasn't changed. */
goto end;
}
/* Free the old local page. */
/* Pass along the new physical address & page id. */
}
end:
return rc;
}
/**
* RTAvlGCPtrDestroy callback.
*
* @returns 0 or VERR_INTERNAL_ERROR.
* @param pNode The node to destroy.
* @param pvGVM The GVM handle.
*/
{
if (pRecVM->pGlobalModule)
{
{
/* Remove from the tree and free memory. */
}
}
return 0;
}
/**
* Used by GMMR0CleanupVM to clean up shared modules.
*
* This is called without taking the GMM lock so that it can be yielded as
* needed here.
*
* @param pGMM The GMM handle.
* @param pGVM The global VM handle.
*/
{
}
#endif /* VBOX_WITH_PAGE_SHARING */
/**
* Removes all shared modules for the specified VM
*
* @returns VBox status code.
* @param pVM VM handle
* @param idCpu VCPU id
*/
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
{
Log(("GMMR0ResetSharedModules\n"));
rc = VINF_SUCCESS;
}
else
return rc;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
#ifdef VBOX_WITH_PAGE_SHARING
typedef struct
{
int rc;
/**
* Tree enumeration callback for checking a shared module.
*/
{
if ( !pLocalModule->fCollision
&& pGlobalModule)
{
Log(("gmmR0CheckSharedModule: check %s %s base=%RGv size=%x collision=%d\n", pGlobalModule->szName, pGlobalModule->szVersion, pGlobalModule->Core.Key, pGlobalModule->cbModule, pLocalModule->fCollision));
pInfo->rc = PGMR0SharedModuleCheck(pInfo->pGVM->pVM, pInfo->pGVM, pInfo->idCpu, pGlobalModule, pLocalModule->cRegions, pLocalModule->aRegions);
return 1; /* stop enumeration. */
}
return 0;
}
#endif /* VBOX_WITH_PAGE_SHARING */
#ifdef DEBUG_sandervl
/**
* Setup for a GMMR0CheckSharedModules call (to allow log flush jumps back to ring 3)
*
* @returns VBox status code.
* @param pVM VM handle
*/
{
/*
* Validate input and get the basics.
*/
/*
* Take the semaphore and do some more validations.
*/
else
rc = VINF_SUCCESS;
return rc;
}
/**
* Clean up after a GMMR0CheckSharedModules call (to allow log flush jumps back to ring 3)
*
* @returns VBox status code.
* @param pVM VM handle
*/
{
/*
* Validate input and get the basics.
*/
return VINF_SUCCESS;
}
#endif /* DEBUG_sandervl */
/**
* Check all shared modules for the specified VM
*
* @returns VBox status code.
* @param pVM VM handle
* @param pVCpu VMCPU handle
*/
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
if (RT_FAILURE(rc))
return rc;
# ifndef DEBUG_sandervl
/*
* Take the semaphore and do some more validations.
*/
# endif
{
Log(("GMMR0CheckSharedModules\n"));
RTAvlGCPtrDoWithAll(&pGVM->gmm.s.pSharedModuleTree, true /* fFromLeft */, gmmR0CheckSharedModule, &Info);
Log(("GMMR0CheckSharedModules done!\n"));
}
else
# ifndef DEBUG_sandervl
# endif
return rc;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
typedef struct
{
bool fFoundDuplicate;
/**
* RTAvlU32DoWithAll callback.
*
* @returns 0
* @param pNode The node to search.
* @param pvInfo Pointer to the input parameters
*/
{
/* Only take chunks not mapped into this VM process; not entirely correct. */
{
if (RT_SUCCESS(rc))
{
/*
* Look for duplicate pages
*/
while (iPage-- > 0)
{
{
{
pInfo->fFoundDuplicate = true;
break;
}
}
}
}
}
}
/**
* Find a duplicate of the specified page in other active VMs
*
* @returns VBox status code.
* @param pVM VM handle
* @param pReq Request packet
*/
{
/*
* Validate input and pass it on.
*/
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
{
if (pChunk)
{
{
if (pPage)
{
Info.fFoundDuplicate = false;
}
else
{
AssertFailed();
}
}
else
AssertFailed();
}
else
AssertFailed();
}
else
return rc;
}
#endif /* VBOX_STRICT && HC_ARCH_BITS == 64 */