alloc-r0drv-linux.c revision 077c71cbbf062c715b834bd6b0d5bd5ddd2ba170
/* $Id$ */
/** @file
* IPRT - Memory Allocation, Ring-0 Driver, Linux.
*/
/*
* Copyright (C) 2006-2010 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"
#if defined(RT_ARCH_AMD64) || defined(DOXYGEN_RUNNING)
/**
* 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
#ifdef RTMEMALLOC_EXEC_HEAP
# include <iprt/spinlock.h>
#endif
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifdef RTMEMALLOC_EXEC_HEAP
# ifdef CONFIG_DEBUG_SET_MODULE_RONX
# define RTMEMALLOC_EXEC_HEAP_VM_AREA 1
# endif
/** The heap. */
/** Spinlock protecting the heap. */
# ifdef RTMEMALLOC_EXEC_HEAP_VM_AREA
static void *g_pvHeap;
# endif
/**
* API for cleaning up the heap spinlock on IPRT termination.
*/
void rtR0MemExecCleanup(void)
{
# ifdef RTMEMALLOC_EXEC_HEAP_VM_AREA
unsigned i;
* map_vm_area() as well as __get_vm_area(). */
if (g_pvHeap)
for (i = 0; i < g_cPages; i++)
__free_page(g_apPages[i]);
# endif
}
# ifndef RTMEMALLOC_EXEC_HEAP_VM_AREA
/**
* 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.
* @param pvMemory Pointer to the memory block.
* @param cb The size of the memory block.
*/
{
int rc;
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
}
return rc;
}
# else /* !RTMEMALLOC_EXEC_HEAP_VM_AREA */
/**
* RTR0MemExecDonate() does not work if CONFIG_DEBUG_SET_MODULE_RONX is enabled.
* In that case, allocate a VM area in the modules range and back it with kernel
* memory. Unfortunately __vmalloc_area() is not exported so we have to emulate
* it.
*/
{
int rc;
unsigned i;
if (RT_SUCCESS(rc))
{
if (!area)
{
return VERR_NO_MEMORY;
}
if (!g_apPages)
{
return VERR_NO_MEMORY;
}
for (i = 0; i < cPages; i++)
{
if (!g_apPages[i])
{
g_cPages = i;
return VERR_NO_MEMORY;
}
}
g_cPages = i;
{
return VERR_NO_MEMORY;
}
if (RT_FAILURE(rc))
}
return rc;
}
# endif /* RTMEMALLOC_EXEC_HEAP_VM_AREA */
#endif /* RTMEMALLOC_EXEC_HEAP */
/**
* 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
# else /* !RTMEMALLOC_EXEC_HEAP */
# endif /* !RTMEMALLOC_EXEC_HEAP */
#else
#endif
}
else
{
{
}
else
}
if (RT_UNLIKELY(!pHdr))
return VERR_NO_MEMORY;
/*
* Initialize.
*/
return VINF_SUCCESS;
}
/**
* OS specific free function.
*/
{
#ifdef RTMEMALLOC_EXEC_HEAP
{
}
#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 ysing 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
}
}
}