MMInternal.h revision 8ca3ccec43a3ed9f0ce0dcbf5c60135bea4e4b74
/* $Id$ */
/** @file
* MM - Internal header file.
*/
/*
* 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.
*/
#ifndef ___MMInternal_h
#define ___MMInternal_h
#include <iprt/critsect.h>
# error "Not in MM! This is an internal header!"
#endif
/** @defgroup grp_mm_int Internals
* @internal
* @ingroup grp_mm
* @{
*/
/** @name VM Ring-3 Heap Internals
* @{
*/
/** @def MMR3HEAP_WITH_STATISTICS
* Enable MMR3Heap statistics.
*/
#if !defined(MMR3HEAP_WITH_STATISTICS) && defined(VBOX_WITH_STATISTICS)
# define MMR3HEAP_WITH_STATISTICS
#endif
/** @def MMR3HEAP_SIZE_ALIGNMENT
* The allocation size alignment of the MMR3Heap.
*/
#define MMR3HEAP_SIZE_ALIGNMENT 16
/**
* Heap statistics record.
* There is one global and one per allocation tag.
*/
typedef struct MMHEAPSTAT
{
/** Core avl node, key is the tag. */
/** Pointer to the heap the memory belongs to. */
#ifdef MMR3HEAP_WITH_STATISTICS
/** Number of allocation. */
/** Number of reallocations. */
/** Number of frees. */
/** Failures. */
/** Number of bytes allocated (sum). */
/** Number of bytes freed. */
/** Number of bytes currently allocated. */
#endif
} MMHEAPSTAT;
/** Pointer to heap statistics record. */
typedef MMHEAPSTAT *PMMHEAPSTAT;
/**
* Additional heap block header for relating allocations to the VM.
*/
typedef struct MMHEAPHDR
{
/** Pointer to the next record. */
/** Pointer to the previous record. */
/** Pointer to the heap statistics record.
* (Where the a PVM can be found.) */
/** Size of the allocation (including this header). */
} MMHEAPHDR;
/** Pointer to MM heap header. */
typedef MMHEAPHDR *PMMHEAPHDR;
/** MM Heap structure. */
typedef struct MMHEAP
{
/** Lock protecting the heap. */
/** Heap block list head. */
/** Heap block list tail. */
/** Heap per tag statistics tree. */
/** The VM handle. */
/** Heap global statistics. */
} MMHEAP;
/** Pointer to MM Heap structure. */
/** @} */
/** @name Hypervisor Heap Internals
* @{
*/
/** @def MMHYPER_HEAP_FREE_DELAY
* If defined, it indicates the number of frees that should be delayed.
*/
#if defined(DOXYGEN_RUNNING)
# define MMHYPER_HEAP_FREE_DELAY 64
#endif
/** @def MMHYPER_HEAP_FREE_POISON
* If defined, it indicates that freed memory should be poisoned
* with the value it has.
*/
#if defined(VBOX_STRICT) || defined(DOXYGEN_RUNNING)
# define MMHYPER_HEAP_FREE_POISON 0xcb
#endif
/** @def MMHYPER_HEAP_STRICT
* Enables a bunch of assertions in the heap code. */
#if defined(VBOX_STRICT) || defined(DOXYGEN_RUNNING)
# define MMHYPER_HEAP_STRICT 1
# if 0 || defined(DOXYGEN_RUNNING)
/** @def MMHYPER_HEAP_STRICT_FENCE
* Enables tail fence. */
# define MMHYPER_HEAP_STRICT_FENCE
/** @def MMHYPER_HEAP_STRICT_FENCE_SIZE
* The fence size in bytes. */
# define MMHYPER_HEAP_STRICT_FENCE_SIZE 256
/** @def MMHYPER_HEAP_STRICT_FENCE_U32
* The fence filler. */
# endif
#endif
/**
* Hypervisor heap statistics record.
* There is one global and one per allocation tag.
*/
typedef struct MMHYPERSTAT
{
/** Core avl node, key is the tag.
* @todo The type is wrong! Get your lazy a$$ over and create that offsetted uint32_t version we need here! */
/** Aligning the 64-bit fields on a 64-bit line. */
/** Indicator for whether these statistics are registered with STAM or not. */
bool fRegistered;
/** Number of allocation. */
/** Number of frees. */
/** Failures. */
/** Number of bytes allocated (sum). */
/** Number of bytes freed (sum). */
/** Number of bytes currently allocated. */
/** Max number of bytes allocated. */
} MMHYPERSTAT;
/** Pointer to hypervisor heap statistics record. */
typedef MMHYPERSTAT *PMMHYPERSTAT;
/**
* Hypervisor heap chunk.
*/
typedef struct MMHYPERCHUNK
{
/** Previous block in the list of all blocks.
* This is relative to the start of the heap. */
/** Offset to the previous block relative to this one. */
/** The statistics record this allocation belongs to (self relative). */
/** Offset to the heap block (self relative). */
} MMHYPERCHUNK;
/** Pointer to a hypervisor heap chunk. */
typedef MMHYPERCHUNK *PMMHYPERCHUNK;
/**
* Hypervisor heap chunk.
*/
typedef struct MMHYPERCHUNKFREE
{
/** Main list. */
/** Offset of the next chunk in the list of free nodes. */
/** Offset of the previous chunk in the list of free nodes. */
/** Size of the block. */
/** Pointer to a free hypervisor heap chunk. */
typedef MMHYPERCHUNKFREE *PMMHYPERCHUNKFREE;
/**
* The hypervisor heap.
*/
typedef struct MMHYPERHEAP
{
/** The typical magic (MMHYPERHEAP_MAGIC). */
/** The heap size. (This structure is not included!) */
/** The HC ring-3 address of the heap. */
/** The HC ring-3 address of the shared VM strcture. */
/** The HC ring-0 address of the heap. */
/** The HC ring-0 address of the shared VM strcture. */
/** The RC address of the heap. */
/** The RC address of the shared VM strcture. */
/** The amount of free memory in the heap. */
/** Offset of the first free chunk in the heap.
* The offset is relative to the start of the heap. */
/** Offset of the last free chunk in the heap.
* The offset is relative to the start of the heap. */
/** Offset of the first page aligned block in the heap.
* The offset is equal to cbHeap initially. */
/** Tree of hypervisor heap statistics. */
#ifdef MMHYPER_HEAP_FREE_DELAY
/** Where to insert the next free. */
/** Array of delayed frees. Circular. Offsets relative to this structure. */
struct
{
/** The free caller address. */
/** The offset of the freed chunk. */
#else
/** Padding the structure to a 64-bit aligned size. */
#endif
} MMHYPERHEAP;
/** Pointer to the hypervisor heap. */
typedef MMHYPERHEAP *PMMHYPERHEAP;
/** Magic value for MMHYPERHEAP. (C. S. Lewis) */
/**
* Hypervisor heap minimum alignment (16 bytes).
*/
#define MMHYPER_HEAP_ALIGN_MIN 16
/**
* The aligned size of the the MMHYPERHEAP structure.
*/
/** @name Hypervisor heap chunk flags.
* The flags are put in the first bits of the MMHYPERCHUNK::offPrev member.
* These bits aren't used anyway because of the chunk minimal alignment (16 bytes).
* @{ */
/** The chunk is free. (The code ASSUMES this is 0!) */
#define MMHYPERCHUNK_FLAGS_FREE 0x0
/** The chunk is in use. */
#define MMHYPERCHUNK_FLAGS_USED 0x1
/** The type mask. */
#define MMHYPERCHUNK_FLAGS_TYPE_MASK 0x1
/** The flag mask */
#define MMHYPERCHUNK_FLAGS_MASK 0x1
/** Checks if the chunk is free. */
#define MMHYPERCHUNK_ISFREE(pChunk) ( (((pChunk)->offPrev) & MMHYPERCHUNK_FLAGS_TYPE_MASK) == MMHYPERCHUNK_FLAGS_FREE )
/** Checks if the chunk is used. */
#define MMHYPERCHUNK_ISUSED(pChunk) ( (((pChunk)->offPrev) & MMHYPERCHUNK_FLAGS_TYPE_MASK) == MMHYPERCHUNK_FLAGS_USED )
#define MMHYPERCHUNK_SET_TYPE(pChunk, type) do { (pChunk)->offPrev = ((pChunk)->offPrev & ~MMHYPERCHUNK_FLAGS_TYPE_MASK) | ((type) & MMHYPERCHUNK_FLAGS_TYPE_MASK); } while (0)
/** Gets the prev offset without the flags. */
/** Sets the prev offset without changing the flags. */
#define MMHYPERCHUNK_SET_OFFPREV(pChunk, off) do { (pChunk)->offPrev = (off) | ((pChunk)->offPrev & MMHYPERCHUNK_FLAGS_MASK); } while (0)
#if 0
/** Clears one or more flags. */
#define MMHYPERCHUNK_FLAGS_OP_CLEAR(pChunk, fFlags) do { ((pChunk)->offPrev) &= ~((fFlags) & MMHYPERCHUNK_FLAGS_MASK); } while (0)
/** Sets one or more flags. */
#define MMHYPERCHUNK_FLAGS_OP_SET(pChunk, fFlags) do { ((pChunk)->offPrev) |= ((fFlags) & MMHYPERCHUNK_FLAGS_MASK); } while (0)
/** Checks if one is set. */
#define MMHYPERCHUNK_FLAGS_OP_ISSET(pChunk, fFlag) (!!(((pChunk)->offPrev) & ((fFlag) & MMHYPERCHUNK_FLAGS_MASK)))
#endif
/** @} */
/** @} */
/** @name Page Pool Internals
* @{
*/
/**
* Page sub pool
*
* About the allocation of this structrue. To keep the number of heap blocks,
* the number of heap calls, and fragmentation low we allocate all the data
* related to a MMPAGESUBPOOL node in one chunk. That means that after the
* bitmap (which is of variable size) comes the SUPPAGE records and then
* follows the lookup tree nodes.
*/
typedef struct MMPAGESUBPOOL
{
/** Pointer to next sub pool. */
struct MMPAGESUBPOOL *pNext;
/** Pointer to next sub pool in the free chain.
* This is NULL if we're not in the free chain or at the end of it. */
struct MMPAGESUBPOOL *pNextFree;
/** Pointer to array of lock ranges.
* This is allocated together with the MMPAGESUBPOOL and thus needs no freeing.
* It follows immediately after the bitmap.
* The reserved field is a pointer to this structure.
*/
/** Pointer to the first page. */
void *pvPages;
/** Size of the subpool. */
unsigned cPages;
/** Number of free pages. */
unsigned cPagesFree;
/** The allocation bitmap.
* This may extend beyond the end of the defined array size.
*/
unsigned auBitmap[1];
/* ... SUPPAGE aRanges[1]; */
/** Pointer to page sub pool. */
typedef MMPAGESUBPOOL *PMMPAGESUBPOOL;
/**
* Page pool.
*/
typedef struct MMPAGEPOOL
{
/** List of subpools. */
/** Head of subpools with free pages. */
/** AVLPV tree for looking up HC virtual addresses.
* The tree contains MMLOOKUPVIRTPP records.
*/
/** Tree for looking up HC physical addresses.
* The tree contains MMLOOKUPPHYSHC records.
*/
/** Pointer to the VM this pool belongs. */
/** Flag indicating the allocation method.
* Set: SUPLowAlloc().
* Clear: SUPPageAlloc() + SUPPageLock(). */
bool fLow;
/** Number of subpools. */
/** Number of pages in pool. */
#ifdef VBOX_WITH_STATISTICS
/** Number of free pages in pool. */
/** Number of alloc calls. */
/** Number of free calls. */
/** Number of to phys conversions. */
/** Number of to virtual conversions. */
/** Number of real errors. */
#endif
} MMPAGEPOOL;
/** Pointer to page pool. */
typedef MMPAGEPOOL *PMMPAGEPOOL;
/**
* Lookup record for HC virtual memory in the page pool.
*/
typedef struct MMPPLOOKUPHCPTR
{
/** The key is virtual address. */
/** Pointer to subpool if lookup record for a pool. */
struct MMPAGESUBPOOL *pSubPool;
/** Pointer to virtual memory lookup record. */
typedef MMPPLOOKUPHCPTR *PMMPPLOOKUPHCPTR;
/**
* Lookup record for HC physical memory.
*/
typedef struct MMPPLOOKUPHCPHYS
{
/** The key is physical address. */
/** Pointer to SUPPAGE record for this physical address. */
/** Pointer to physical memory lookup record. */
typedef MMPPLOOKUPHCPHYS *PMMPPLOOKUPHCPHYS;
/** @} */
/**
* Type of memory that's locked.
*/
typedef enum MMLOCKEDTYPE
{
/** Hypervisor: Ring-3 memory locked by MM. */
/** Hypervisor: Ring-3 memory locked by MM that shouldn't be freed up. */
/** Hypervisor: Pre-locked ring-3 pages. */
/** Guest: Physical VM memory (RAM & MMIO2). */
} MMLOCKEDTYPE;
/** Pointer to memory type. */
typedef MMLOCKEDTYPE *PMMLOCKEDTYPE;
/**
* Converts a SUPPAGE pointer to a MMLOCKEDMEM pointer.
* @returns Pointer to the MMLOCKEDMEM record the range is associated with.
* @param pSupPage Pointer to SUPPAGE structure managed by MM.
*/
/**
* Locked memory record.
*/
typedef struct MMLOCKEDMEM
{
/** Address (host mapping). */
void *pv;
/** Size. */
/** Next record. */
struct MMLOCKEDMEM *pNext;
/** Record type. */
/** Type specific data. */
union
{
/** Data for MM_LOCKED_TYPE_HYPER, MM_LOCKED_TYPE_HYPER_NOFREE and MM_LOCKED_TYPE_HYPER_PAGES. */
struct
{
unsigned uNothing;
} hyper;
/** Data for MM_LOCKED_TYPE_PHYS. */
struct
{
/** The GC physical address.
* (Assuming that this is a linear range of GC physical pages.)
*/
} phys;
} u;
/** Physical Page Array. (Variable length.)
* The uReserved field contains pointer to the MMLOCKMEM record.
* Use the macro MM_SUPPAGE_TO_MMLOCKEDMEM() to convert.
*
* For MM_LOCKED_TYPE_PHYS the low 12 bits of the pvPhys member
* are bits (MM_RAM_FLAGS_*) and not part of the physical address.
*/
} MMLOCKEDMEM;
/** Pointer to locked memory. */
typedef MMLOCKEDMEM *PMMLOCKEDMEM;
/**
* A registered Rom range.
*
* This is used to track ROM registrations both for debug reasons
* and for resetting shadow ROM at reset.
*
* This is allocated of the MMR3Heap and thus only accessibel from ring-3.
*/
typedef struct MMROMRANGE
{
/** Pointer to the next */
struct MMROMRANGE *pNext;
/** Address of the range. */
/** Size of the range. */
/** Shadow ROM? */
bool fShadow;
/** Is the shadow ROM currently wriable? */
bool fWritable;
/** The address of the virgin ROM image for shadow ROM. */
const void *pvBinary;
/** The address of the guest RAM that's shadowing the ROM. (lazy bird) */
void *pvCopy;
/** The ROM description. */
const char *pszDesc;
} MMROMRANGE;
/** Pointer to a ROM range. */
typedef MMROMRANGE *PMMROMRANGE;
/**
* Hypervisor memory mapping type.
*/
typedef enum MMLOOKUPHYPERTYPE
{
/** Invalid record. This is used for record which are incomplete. */
/** Mapping of locked memory. */
/** Mapping of contiguous HC physical memory. */
/** Mapping of contiguous GC physical memory. */
/** Mapping of MMIO2 memory. */
/** Dynamic mapping area (MMR3HyperReserve).
* A conversion will require to check what's in the page table for the pages. */
/**
* Lookup record for the hypervisor memory area.
*/
typedef struct MMLOOKUPHYPER
{
/** Byte offset from the start of this record to the next.
* If the value is NIL_OFFSET the chain is terminated. */
/** Offset into the hypvervisor memory area. */
/** Size of this part. */
/** Locking type. */
/** Type specific data */
union
{
/** Locked memory. */
struct
{
/** Host context pointer. */
/** Host context ring-0 pointer. */
/** Pointer to the locked mem record. */
} Locked;
/** Contiguous physical memory. */
struct
{
/** Host context pointer. */
/** HC physical address corresponding to pvR3. */
} HCPhys;
/** Contiguous guest physical memory. */
struct
{
/** The memory address (Guest Context). */
} GCPhys;
/** MMIO2 memory. */
struct
{
/** The device instance owning the MMIO2 region. */
/** The region number. */
/** The offset into the MMIO2 region. */
} MMIO2;
} u;
/** Description. */
/** Pointer to a hypervisor memory lookup record. */
typedef MMLOOKUPHYPER *PMMLOOKUPHYPER;
/**
* Converts a MM pointer into a VM pointer.
* @returns Pointer to the VM structure the MM is part of.
* @param pMM Pointer to MM instance data.
*/
/**
* MM Data (part of VM)
*/
typedef struct MM
{
/** Offset to the VM structure.
* See MM2VM(). */
/** Set if MMR3InitPaging has been called. */
bool fDoneMMR3InitPaging;
/** Set if PGM has been initialized and we can safely call PGMR3Map(). */
bool fPGMInitialized;
#endif
/** Lookup list for the Hypervisor Memory Area.
* The offset is relative to the start of the heap.
* Use pHyperHeapHC or pHyperHeapGC to calculate the address.
*/
/** The offset of the next static mapping in the Hypervisor Memory Area. */
/** The size of the HMA.
* Starts at 12MB and will be fixed late in the init process. */
/** Guest address of the Hypervisor Memory Area.
* @remarks It's still a bit open whether this should be change to RTRCPTR or
* remain a RTGCPTR. */
/** The hypervisor heap (GC Ptr). */
#endif
/** The hypervisor heap (R3 Ptr). */
/** The hypervisor heap (R0 Ptr). */
/** List of memory locks. (HC only) */
/** Page pool. (HC only) */
/** Page pool pages in low memory. (HC only) */
/** Pointer to the dummy page.
* The dummy page is a paranoia thingy used for instance for pure MMIO RAM ranges
* to make sure any bugs will not harm whatever the system stores in the first
* physical page. */
R3PTRTYPE(void *) pvDummyPage;
/** Physical address of the dummy page. */
/** Size of the base RAM in bytes. (The CFGM RamSize value.) */
/** The number of base RAM pages that PGM has reserved (GMM).
* @remarks Shadow ROMs will be counted twice (RAM+ROM), so it won't be 1:1 with
* what the guest sees. */
/** The number of shadow pages PGM has reserved (GMM). */
/** The number of fixed pages we've reserved (GMM). */
/** The head of the ROM ranges. */
} MM;
/** Pointer to MM Data (part of VM). */
/**
* MM data kept in the UVM.
*/
typedef struct MMUSERPERVM
{
/** Pointer to the MM R3 Heap. */
} MMUSERPERVM;
/** Pointer to the MM data kept in the UVM. */
typedef MMUSERPERVM *PMMUSERPERVM;
int mmR3LockMem(PVM pVM, void *pv, size_t cb, MMLOCKEDTYPE eType, PMMLOCKEDMEM *ppLockedMem, bool fSilentFailure);
int mmR3MapLocked(PVM pVM, PMMLOCKEDMEM pLockedMem, RTGCPTR Addr, unsigned iPage, size_t cPages, unsigned fFlags);
/**
* Converts a pool address to a physical address.
* The specified allocation type must match with the address.
*
* @returns Physical address.
* @returns NIL_RTHCPHYS if not found or eType is not matching.
* @param pPool Pointer to the page pool.
* @param pv The address to convert.
* @thread The Emulation Thread.
*/
/**
* Converts a pool physical address to a linear address.
* The specified allocation type must match with the address.
*
* @returns Physical address.
* @returns NULL if not found or eType is not matching.
* @param pPool Pointer to the page pool.
* @param HCPhys The address to convert.
* @thread The Emulation Thread.
*/
/** @} */
#endif