memobj-r0drv.cpp revision aaec12d462b9cd40af20dcb3994c88fa4f108712
/* $Id$ */
/** @file
* InnoTek Portable Runtime - Ring-0 Memory Objects, Common Code.
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/**
* Internal function for allocating a new memory object.
*
* @returns The allocated and initialized handle.
* @param cbSelf The size of the memory object handle. 0 mean default size.
* @param enmType The memory object type.
* @param pv The memory object mapping.
* @param cb The size of the memory object.
*/
{
/* validate the size */
if (!cbSelf)
/*
* Allocate and initialize the object.
*/
if (pNew)
{
}
return pNew;
}
/**
* Links a mapping object to a primary object.
*
* @returns IPRT status code.
* @retval VINF_SUCCESS on success.
* @retval VINF_NO_MEMORY if we couldn't expand the mapping array of the parent.
* @param pParent The parent (primary) memory object.
* @param pChild The child (mapping) memory object.
*/
{
/* sanity */
/* expand the array? */
{
if (!pv)
return VERR_NO_MEMORY;
}
/* do the linking. */
return VINF_SUCCESS;
}
/**
* Checks if this is mapping or not.
*
* @returns true if it's a mapping, otherwise false.
* @param MemObj The ring-0 memory object handle.
*/
{
/* Validate the object handle. */
AssertPtrReturn(MemObj, false);
/* hand it on to the inlined worker. */
return rtR0MemObjIsMapping(pMem);
}
/**
* Gets the address of a ring-0 memory object.
*
* @returns The address of the memory object.
* @returns NULL if the handle is invalid (asserts in strict builds) or if there isn't any mapping.
* @param MemObj The ring-0 memory object handle.
*/
{
/* Validate the object handle. */
AssertPtrReturn(MemObj, 0);
/* return the mapping address. */
}
/**
* Gets the size of a ring-0 memory object.
*
* @returns The address of the memory object.
* @returns NULL if the handle is invalid (asserts in strict builds) or if there isn't any mapping.
* @param MemObj The ring-0 memory object handle.
*/
{
/* Validate the object handle. */
AssertPtrReturn(MemObj, 0);
/* return the size. */
}
/**
* Get the physical address of an page in the memory object.
*
* @returns The physical address.
* @returns NIL_RTHCPHYS if the object doesn't contain fixed physical pages.
* @returns NIL_RTHCPHYS if the iPage is out of range.
* @returns NIL_RTHCPHYS if the object handle isn't valid.
* @param MemObj The ring-0 memory object handle.
* @param iPage The page number within the object.
*/
{
/* Validate the object handle. */
AssertReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, NIL_RTHCPHYS);
{
/* permit: while (RTR0MemObjGetPagePhysAddr(pMem, iPage++) != NIL_RTHCPHYS) {} */
return NIL_RTHCPHYS;
}
/*
* We know the address of physically contiguous allocations and mappings.
*/
/*
* Do the job.
*/
}
/**
* Frees a ring-0 memory object.
*
* @returns IPRT status code.
* @retval VERR_INVALID_HANDLE if
* @param MemObj The ring-0 memory object to be freed. NULL is accepted.
* @param fFreeMappings Whether or not to free mappings of the object.
*/
{
/*
* Validate the object handle.
*/
if (MemObj == NIL_RTR0MEMOBJ)
return VINF_SUCCESS;
AssertReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
/*
* Deal with mapings according to fFreeMappings.
*/
if ( !rtR0MemObjIsMapping(pMem)
{
/* fail if not requested to free mappings. */
if (!fFreeMappings)
return VERR_MEMORY_BUSY;
{
/* sanity checks. */
/* free the mapping. */
if (RT_FAILURE(rc))
{
Log(("RTR0MemObjFree: failed to free mapping %p: %p %#zx; rc=%Vrc\n", pChild, pChild->pv, pChild->cb, rc));
return rc;
}
}
}
/*
* Free this object.
*/
if (RT_SUCCESS(rc))
{
/*
* Ok, it was freed just fine. Now, if it's a mapping we'll have to remove it from the parent.
*/
if (rtR0MemObjIsMapping(pMem))
{
/* sanity checks */
/* locate and remove from the array of mappings. */
while (i-- > 0)
{
{
pParent->uRel.Parent.papMappings[i] = pParent->uRel.Parent.papMappings[--pParent->uRel.Parent.cMappings];
break;
}
}
Assert(i != UINT32_MAX);
}
else
/*
* Finally, destroy the handle.
*/
if (!rtR0MemObjIsMapping(pMem))
}
else
Log(("RTR0MemObjFree: failed to free %p: %d %p %#zx; rc=%Vrc\n",
return rc;
}
/**
* Allocates page aligned virtual kernel memory.
*
* The memory is taken from a non paged (= fixed physical memory backing) pool.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle.
* @param cb Number of bytes to allocate. This is rounded up to nearest page.
* @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
*/
{
/* sanity checks. */
/* do the allocation. */
}
/**
* Allocates page aligned virtual kernel memory with physical backing below 4GB.
*
* The physical memory backing the allocation is fixed.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle.
* @param cb Number of bytes to allocate. This is rounded up to nearest page.
* @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
*/
{
/* sanity checks. */
/* do the allocation. */
}
/**
* Allocates page aligned virtual kernel memory with contiguous physical backing below 4GB.
*
* The physical memory backing the allocation is fixed.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle.
* @param cb Number of bytes to allocate. This is rounded up to nearest page.
* @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
*/
{
/* sanity checks. */
/* do the allocation. */
}
/**
* Locks a range of user virtual memory.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle.
* @param pv User virtual address. This is rounded down to a page boundrary.
* @param cb Number of bytes to lock. This is rounded up to nearest page boundrary.
* @param R0Process The process to lock pages in. NIL_R0PROCESS is an alias for the current one.
*
* @remark RTR0MemObjGetAddress() will return the rounded down address.
*/
{
/* sanity checks. */
if (R0Process == NIL_RTR0PROCESS)
/* do the allocation. */
}
/**
* Locks a range of kernel virtual memory.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle.
* @param pv Kernel virtual address. This is rounded down to a page boundrary.
* @param cb Number of bytes to lock. This is rounded up to nearest page boundrary.
*
* @remark RTR0MemObjGetAddress() will return the rounded down address.
*/
{
/* sanity checks. */
/* do the allocation. */
}
/**
* Allocates page aligned physical memory without (necessarily) any kernel mapping.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle.
* @param cb Number of bytes to allocate. This is rounded up to nearest page.
* @param PhysHighest The highest permittable address (inclusive).
* Pass NIL_RTHCPHYS if any address is acceptable.
*/
{
/* sanity checks. */
/* do the allocation. */
}
/**
* Creates a page aligned, contiguous, physical memory object.
*
* No physical memory is allocated, we trust you do know what you're doing.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle.
* @param Phys The physical address to start at. This is rounded down to the
* nearest page boundrary.
* @param cb The size of the object in bytes. This is rounded up to nearest page boundrary.
*/
{
/* sanity checks. */
/* do the allocation. */
}
/**
* Reserves kernel virtual address space.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle.
* @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
* @param cb The number of bytes to reserve. This is rounded up to nearest page.
* @param uAlignment The alignment of the reserved memory.
* Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
*/
RTR0DECL(int) RTR0MemObjReserveKernel(PRTR0MEMOBJ pMemObj, void *pvFixed, size_t cb, size_t uAlignment)
{
/* sanity checks. */
if (uAlignment == 0)
AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
if (pvFixed != (void *)-1)
/* do the reservation. */
}
/**
* Reserves user virtual address space in the current process.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle.
* @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
* @param cb The number of bytes to reserve. This is rounded up to nearest PAGE_SIZE.
* @param uAlignment The alignment of the reserved memory.
* Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
* @param R0Process The process to reserve the memory in. NIL_R0PROCESS is an alias for the current one.
*/
RTR0DECL(int) RTR0MemObjReserveUser(PRTR0MEMOBJ pMemObj, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
{
/* sanity checks. */
if (uAlignment == 0)
AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
if (pvFixed != (void *)-1)
if (R0Process == NIL_RTR0PROCESS)
/* do the reservation. */
}
/**
* Maps a memory object into kernel virtual address space.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
* @param MemObjToMap The object to be map.
* @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
* @param uAlignment The alignment of the reserved memory.
* Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
* @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
*/
RTR0DECL(int) RTR0MemObjMapKernel(PRTR0MEMOBJ pMemObj, PRTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
{
/* sanity checks. */
AssertReturn(pMemToMap->enmType > RTR0MEMOBJTYPE_INVALID && pMemToMap->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
if (uAlignment == 0)
AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
if (pvFixed != (void *)-1)
AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
/* do the mapping. */
if (RT_SUCCESS(rc))
{
/* link it. */
if (RT_SUCCESS(rc))
else
{
/* damn, out of memory. bail out. */
}
}
return rc;
}
/**
* Maps a memory object into user virtual address space in the current process.
*
* @returns IPRT status code.
* @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
* @param MemObjToMap The object to be map.
* @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
* @param uAlignment The alignment of the reserved memory.
* Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
* @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
* @param R0Process The process to map the memory into. NIL_R0PROCESS is an alias for the current one.
*/
RTR0DECL(int) RTR0MemObjMapUser(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
{
/* sanity checks. */
AssertReturn(pMemToMap->enmType > RTR0MEMOBJTYPE_INVALID && pMemToMap->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
if (uAlignment == 0)
AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
if (pvFixed != (void *)-1)
AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
if (R0Process == NIL_RTR0PROCESS)
/* do the mapping. */
if (RT_SUCCESS(rc))
{
/* link it. */
if (RT_SUCCESS(rc))
else
{
/* damn, out of memory. bail out. */
}
}
return rc;
}