Index: trunk/src/VBox/Runtime/Makefile
===================================================================
--- trunk/src/VBox/Runtime/Makefile	(revision 288)
+++ trunk/src/VBox/Runtime/Makefile	(revision 289)
@@ -546,4 +546,6 @@
 	r0drv/linux/spinlock-r0drv-linux.c \
 	r0drv/linux/thread-r0drv-linux.c
+RuntimeR0Drv_SOURCES.linux.amd64 = \
+	alloc/heapsimple.cpp
 
 RuntimeR0Drv_SOURCES.win = \
Index: trunk/src/VBox/Runtime/r0drv/alloc-r0drv.h
===================================================================
--- trunk/src/VBox/Runtime/r0drv/alloc-r0drv.h	(revision 288)
+++ trunk/src/VBox/Runtime/r0drv/alloc-r0drv.h	(revision 289)
@@ -51,4 +51,5 @@
 #define RTMEMHDR_FLAG_EXEC      BIT(1)
 #ifdef __LINUX__
+#define RTMEMHDR_FLAG_EXEC_HEAP BIT(30)
 #define RTMEMHDR_FLAG_KMALLOC   BIT(31)
 #endif
Index: trunk/src/VBox/Runtime/r0drv/linux/alloc-r0drv-linux.c
===================================================================
--- trunk/src/VBox/Runtime/r0drv/linux/alloc-r0drv-linux.c	(revision 288)
+++ trunk/src/VBox/Runtime/r0drv/linux/alloc-r0drv-linux.c	(revision 289)
@@ -29,4 +29,72 @@
 #include "r0drv/alloc-r0drv.h"
 
+#if defined(__AMD64__) || defined(__DOXYGEN__)
+/**
+ * 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/heap.h>
+# include <iprt/spinlock.h>
+#endif
+
+
+/*******************************************************************************
+*   Global Variables                                                           *
+*******************************************************************************/
+#ifdef RTMEMALLOC_EXEC_HEAP
+/** The heap. */
+static RTHEAPSIMPLE g_HeapExec = NIL_RTHEAPSIMPLE;
+/** Spinlock protecting the heap. */
+static RTSPINLOCK   g_HeapExecSpinlock = NIL_RTHEAPSIMPLE;
+
+
+/**
+ * API for cleaning up the heap spinlock on IPRT termination.
+ * This is as RTMemExecDonate specific to AMD64 Linux/GNU.
+ */
+RTDECL(void) RTMemExecCleanup(void)
+{
+    RTSpinlockDestroy(g_HeapSpinlock);
+    g_HeapSpinlock = NIL_RTSPINLOCK;
+}
+
+
+/**
+ * Donate read+write+execute memory to the exec heap.
+ *
+ * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
+ * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
+ * 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.
+ */
+RTDECL(int) RTMemExecDonate(void *pvMemory, size_t cb)
+{
+    AssertReturn(g_HeapExec == NIL_RTHEAPSIMPLE, VERR_WRONG_ORDER);
+
+    int rc = RTSpinlockCreate(&g_HeapExecSpinlock);
+    if (RT_SUCCESS(rc))
+    {
+        rc = RTHeapSimpleInit(&g_HeapExec, pvMemory, cb);
+        if (RT_FAILURE(rc))
+            RTMemExecCleanup();
+    }
+    return rc;
+}
+#endif /* RTMEMALLOC_EXEC_HEAP */
+
+
 
 /**
@@ -43,37 +111,16 @@
     {
 #if defined(__AMD64__)
-#if 0
-        /*
-         * 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!
-         */
-        static uint8_t      s_abMemory[_2M];
-        static uint8_t     *s_pbMemory = NULL; /**< NULL if not initialized. */
-        static RTSPINLOCK   s_Spinlock = NIL_RTSPINLOCK;
-        static struct RTMEMRECLNXEXEC
+# ifdef RTMEMALLOC_EXEC_HEAP
+        if (g_HeapExec != NIL_RTHEAPSIMPLE)
         {
-            PRTMEMHDR   pHdr;   /**< NULL if no free range. */
-            uint32_t    off;    /**< Offset into s_pbMemory. */
-            uint32_t    cb;     
-        }                   s_aMemRecs[64];
-        RTSPINLOCKTMP       SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
-
-        if (!s_pbMemory) 
-        {
-            /* serialize */
+            fFlags |= RTMEMHDR_FLAG_EXEC_HEAP;
+            RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
+            RTSpinlockAcquireNoInts(g_HeapExecSpinlock, &SpinlockTmp);
+            pHdr = (PRTMEMHDR)RTHeapSimpleAlloc(g_HeapExec, cb + sizeof(*pHdr), 0);
+            RTSpinlockReleaseNoInts(g_HeapExecSpinlock, &SpinlockTmp);
         }
-
-        RTSpinlockAcquireNoInts(s_Spinlock, &SpinlockTmp);
-        /* find free area and split it... */
-
-        RTSpinlockReleaseNoInts(s_Spinlock, &SpinlockTmp);
-
-# else
-        pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
-# endif 
-
+        else
+# endif
+            pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
 
 #elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
@@ -83,5 +130,4 @@
         pHdr = (PRTMEMHDR)vmalloc(cb + sizeof(*pHdr));
 #endif
-        fFlags &= ~RTMEMHDR_FLAG_KMALLOC;
     }
     else
@@ -93,8 +139,5 @@
         }
         else
-        {
-            fFlags &= ~RTMEMHDR_FLAG_KMALLOC;
             pHdr = vmalloc(cb + sizeof(*pHdr));
-        }
     }
 
@@ -121,4 +164,12 @@
     if (pHdr->fFlags & RTMEMHDR_FLAG_KMALLOC)
         kfree(pHdr);
+#ifdef RTMEMALLOC_EXEC_HEAP
+    else if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC_HEAP)
+    {
+        RTSpinlockAcquireNoInts(g_HeapExecSpinlock, &SpinlockTmp);
+        RTHeapSimpleFree(g_HeapExec, pHdr);
+        RTSpinlockReleaseNoInts(g_HeapExecSpinlock, &SpinlockTmp);
+    }
+#endif
     else
         vfree(pHdr);
