alloc-r0drv-linux.c revision 437a23933c78f7ba06f4a746543db53d15fba9fe
/* $Id$ */
/** @file
* IPRT - Memory Allocation, Ring-0 Driver, Linux.
*/
/*
* Copyright (C) 2006-2012 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.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "the-linux-kernel.h"
#include "r0drv/alloc-r0drv.h"
/**
* Starting with 2.6.23 we can use __get_vm_area and map_vm_area to allocate
* memory in the moduel range. This is preferrable to the exec heap below.
*/
# define RTMEMALLOC_EXEC_VM_AREA
# else
/**
* We need memory in the module range (~2GB to ~0) this can only be obtained
* thru APIs that are not exported (see module_alloc()).
*
* So, we'll have to create a quick and dirty heap here using BSS memory.
* Very annoying and it's going to restrict us!
*/
# define RTMEMALLOC_EXEC_HEAP
# endif
#endif
#ifdef RTMEMALLOC_EXEC_HEAP
# include <iprt/spinlock.h>
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
#ifdef RTMEMALLOC_EXEC_VM_AREA
/**
* Extended header used for headers marked with RTMEMHDR_FLAG_EXEC_VM_AREA.
*
* This is used with allocating executable memory, for things like generated
* code and loaded modules.
*/
typedef struct RTMEMLNXHDREX
{
/** The VM area for this allocation. */
void *pvDummy;
/** The header we present to the generic API. */
/** Pointer to an extended memory header. */
typedef RTMEMLNXHDREX *PRTMEMLNXHDREX;
#endif
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifdef RTMEMALLOC_EXEC_HEAP
/** The heap. */
/** Spinlock protecting the heap. */
#endif
/**
* API for cleaning up the heap spinlock on IPRT termination.
*/
DECLHIDDEN(void) rtR0MemExecCleanup(void)
{
#ifdef RTMEMALLOC_EXEC_HEAP
#endif
}
/**
* Donate read+write+execute memory to the exec heap.
*
* allocated memory in the module if it wishes for GCC generated code to work.
* GCC can only generate modules that work in the address range ~2GB to ~0
* currently.
*
* The API only accept one single donation.
*
* @returns IPRT status code.
* @retval VERR_NOT_SUPPORTED if the code isn't enabled.
* @param pvMemory Pointer to the memory block.
* @param cb The size of the memory block.
*/
{
#ifdef RTMEMALLOC_EXEC_HEAP
int rc;
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
}
return rc;
#else
return VERR_NOT_SUPPORTED;
#endif
}
#ifdef RTMEMALLOC_EXEC_VM_AREA
/**
* Allocate executable kernel memory in the module range.
*
* @returns Pointer to a allocation header success. NULL on failure.
*
* @param cb The size the user requested.
*/
{
if (!pVmArea)
return NULL;
if (!papPages)
{
return NULL;
}
{
break;
}
{
/*
* Map the pages. The API requires an iterator argument, which can be
* used, in case of failure, to figure out how much was actually
* mapped. Not sure how useful this really is, but whatever.
*
* Not entirely sure we really need to set nr_pages and pages here, but
* they provide a very convenient place for storing something we need
* in the free function, if nothing else...
*/
{
}
/* bail out */
}
while (iPage-- > 0)
return NULL;
}
#endif /* RTMEMALLOC_EXEC_VM_AREA */
/**
* OS specific allocation function.
*/
{
/*
* Allocate.
*/
if (fFlags & RTMEMHDR_FLAG_EXEC)
{
if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
return VERR_NOT_SUPPORTED;
#if defined(RT_ARCH_AMD64)
# ifdef RTMEMALLOC_EXEC_HEAP
if (g_HeapExec != NIL_RTHEAPSIMPLE)
{
}
else
# elif defined(RTMEMALLOC_EXEC_VM_AREA)
# else /* !RTMEMALLOC_EXEC_HEAP */
# error "you don not want to go here..."
# endif /* !RTMEMALLOC_EXEC_HEAP */
#else
#endif
}
else
{
if (
#if 1 /* vmalloc has serious performance issues, avoid it. */
#else
#endif
|| (fFlags & RTMEMHDR_FLAG_ANY_CTX)
)
{
if (RT_UNLIKELY( !pHdr
&& !(fFlags & RTMEMHDR_FLAG_ANY_CTX) ))
{
}
}
else
}
if (RT_UNLIKELY(!pHdr))
return VERR_NO_MEMORY;
/*
* Initialize.
*/
return VINF_SUCCESS;
}
/**
* OS specific free function.
*/
{
#ifdef RTMEMALLOC_EXEC_HEAP
{
}
#endif
#ifdef RTMEMALLOC_EXEC_VM_AREA
{
while (iPage-- > 0)
}
#endif
else
}
/**
* Compute order. Some functions allocate 2^order pages.
*
* @returns order.
* @param cPages Number of pages.
*/
static int CalcPowerOf2Order(unsigned long cPages)
{
int iOrder;
unsigned long cTmp;
;
++iOrder;
return iOrder;
}
/**
* Allocates physical contiguous memory (below 4GB).
* The allocation is page aligned and the content is undefined.
*
* @returns Pointer to the memory block. This is page aligned.
* @param pPhys Where to store the physical address.
* @param cb The allocation size in bytes. This is always
* rounded up to PAGE_SIZE.
*/
{
int cOrder;
unsigned cPages;
/*
* validate input.
*/
/*
* Allocate page pointer array.
*/
/* ZONE_DMA32: 0-4GB */
if (!paPages)
#endif
#ifdef RT_ARCH_AMD64
/* ZONE_DMA; 0-16MB */
#else
/* ZONE_NORMAL: 0-896MB */
#endif
if (paPages)
{
/*
* Reserve the pages and mark them executable.
*/
unsigned iPage;
{
{
}
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 20) /** @todo find the exact kernel where change_page_attr was introduced. */
#endif
}
}
return NULL;
}
/**
* Frees memory allocated using RTMemContAlloc().
*
* @param pv Pointer to return from RTMemContAlloc().
* @param cb The cb parameter passed to RTMemContAlloc().
*/
{
if (pv)
{
int cOrder;
unsigned cPages;
unsigned iPage;
/* validate */
/* calc order and get pages */
/*
* Restore page attributes freeing the pages.
*/
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 20) /** @todo find the exact kernel where change_page_attr was introduced. */
#endif
}
}
}