alloc-r0drv-linux.c revision 501311711d2beab90b719964f7f1c68bb3d37c85
0f70ed40798198e1d9099c6ae3bdb239d2b8cf0dvboxsync * InnoTek Portable Runtime - Memory Allocation, Ring-0 Driver, Linux.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Copyright (C) 2006 InnoTek Systemberatung GmbH
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * available from http://www.virtualbox.org. This file is free software;
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * you can redistribute it and/or modify it under the terms of the GNU
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * General Public License as published by the Free Software Foundation,
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * If you received this file as part of a commercial VirtualBox
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * distribution, then only the terms of your commercial VirtualBox
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * license agreement apply instead of the previous paragraph.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/*******************************************************************************
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync* Header Files *
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync*******************************************************************************/
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * We need memory in the module range (~2GB to ~0) this can only be obtained
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * thru APIs that are not exported (see module_alloc()).
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * So, we'll have to create a quick and dirty heap here using BSS memory.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Very annoying and it's going to restrict us!
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/*******************************************************************************
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync* Global Variables *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync*******************************************************************************/
500aaaf3dc1d98456808e7618db3fb2e7c8fb8e0vboxsync/** The heap. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/** Spinlock protecting the heap. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncstatic RTSPINLOCK g_HeapExecSpinlock = NIL_RTHEAPSIMPLE;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * API for cleaning up the heap spinlock on IPRT termination.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * This is as RTMemExecDonate specific to AMD64 Linux/GNU.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Donate read+write+execute memory to the exec heap.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * allocated memory in the module if it wishes for GCC generated code to work.
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * GCC can only generate modules that work in the address range ~2GB to ~0
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * currently.
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync * The API only accept one single donation.
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync * @returns IPRT status code.
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync * @param pvMemory Pointer to the memory block.
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync * @param cb The size of the memory block.
e2843ed205192b88e54eef60ad541d00bbbc932avboxsyncRTDECL(int) RTMemExecDonate(void *pvMemory, size_t cb)
77da7a074c86956d36759983037056c00cb87535vboxsync AssertReturn(g_HeapExec == NIL_RTHEAPSIMPLE, VERR_WRONG_ORDER);
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync#endif /* RTMEMALLOC_EXEC_HEAP */
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * OS specific allocation function.
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync * Allocate.
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync Assert(cb != sizeof(void *)); /* 99% of pointer sized allocations are wrong. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTSpinlockAcquireNoInts(g_HeapExecSpinlock, &SpinlockTmp);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync pHdr = (PRTMEMHDR)RTHeapSimpleAlloc(g_HeapExec, cb + sizeof(*pHdr), 0);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTSpinlockReleaseNoInts(g_HeapExecSpinlock, &SpinlockTmp);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM,
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync __pgprot(cpu_has_pge ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Initialize.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * OS specific free function.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTSpinlockAcquireNoInts(g_HeapExecSpinlock, &SpinlockTmp);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTSpinlockReleaseNoInts(g_HeapExecSpinlock, &SpinlockTmp);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Compute order. Some functions allocate 2^order pages.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @returns order.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @param cPages Number of pages.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync unsigned long cTmp;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Allocates physical contiguous memory (below 4GB).
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * The allocation is page aligned and the content is undefined.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @returns Pointer to the memory block. This is page aligned.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @param pPhys Where to store the physical address.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @param cb The allocation size in bytes. This is always
77da7a074c86956d36759983037056c00cb87535vboxsync * rounded up to PAGE_SIZE.
77da7a074c86956d36759983037056c00cb87535vboxsyncRTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * validate input.
8ffcab9595cc0d56977968cd496363502fd814aevboxsync * Allocate page pointer array.
2e2dec6e64c09dd7e3fe4ad0ee8bb5cf7d63762evboxsync#ifdef __AMD64__ /** @todo check out if there is a correct way of getting memory below 4GB (physically). */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Reserve the pages and mark them executable.
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync AssertMsg( (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage])) + PAGE_SIZE
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync == (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage + 1]))
e2843ed205192b88e54eef60ad541d00bbbc932avboxsync ("iPage=%i cPages=%u [0]=%#llx,%p [1]=%#llx,%p\n", iPage, cPages,
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync (long long)page_to_phys(&paPages[iPage]), phys_to_virt(page_to_phys(&paPages[iPage])),
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync (long long)page_to_phys(&paPages[iPage + 1]), phys_to_virt(page_to_phys(&paPages[iPage + 1])) ));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync MY_CHANGE_PAGE_ATTR(&paPages[iPage], 1, MY_PAGE_KERNEL_EXEC);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Frees memory allocated ysing RTMemContAlloc().
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @param pv Pointer to return from RTMemContAlloc().
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @param cb The cb parameter passed to RTMemContAlloc().
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /* validate */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /* calc order and get pages */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Restore page attributes freeing the pages.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))