VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/alloc-r0drv-linux.c@ 6103

Last change on this file since 6103 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1/* $Id: alloc-r0drv-linux.c 5999 2007-12-07 15:05:06Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Memory Allocation, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "the-linux-kernel.h"
32#include <iprt/mem.h>
33#include <iprt/assert.h>
34#include "r0drv/alloc-r0drv.h"
35
36#if defined(RT_ARCH_AMD64) || defined(__DOXYGEN__)
37/**
38 * We need memory in the module range (~2GB to ~0) this can only be obtained
39 * thru APIs that are not exported (see module_alloc()).
40 *
41 * So, we'll have to create a quick and dirty heap here using BSS memory.
42 * Very annoying and it's going to restrict us!
43 */
44# define RTMEMALLOC_EXEC_HEAP
45#endif
46#ifdef RTMEMALLOC_EXEC_HEAP
47# include <iprt/heap.h>
48# include <iprt/spinlock.h>
49# include <iprt/err.h>
50#endif
51
52
53/*******************************************************************************
54* Global Variables *
55*******************************************************************************/
56#ifdef RTMEMALLOC_EXEC_HEAP
57/** The heap. */
58static RTHEAPSIMPLE g_HeapExec = NIL_RTHEAPSIMPLE;
59/** Spinlock protecting the heap. */
60static RTSPINLOCK g_HeapExecSpinlock = NIL_RTSPINLOCK;
61
62
63/**
64 * API for cleaning up the heap spinlock on IPRT termination.
65 * This is as RTMemExecDonate specific to AMD64 Linux/GNU.
66 */
67void rtR0MemExecCleanup(void)
68{
69 RTSpinlockDestroy(g_HeapExecSpinlock);
70 g_HeapExecSpinlock = NIL_RTSPINLOCK;
71}
72
73
74/**
75 * Donate read+write+execute memory to the exec heap.
76 *
77 * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
78 * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
79 * allocated memory in the module if it wishes for GCC generated code to work.
80 * GCC can only generate modules that work in the address range ~2GB to ~0
81 * currently.
82 *
83 * The API only accept one single donation.
84 *
85 * @returns IPRT status code.
86 * @param pvMemory Pointer to the memory block.
87 * @param cb The size of the memory block.
88 */
89RTR0DECL(int) RTR0MemExecDonate(void *pvMemory, size_t cb)
90{
91 int rc;
92 AssertReturn(g_HeapExec == NIL_RTHEAPSIMPLE, VERR_WRONG_ORDER);
93
94 rc = RTSpinlockCreate(&g_HeapExecSpinlock);
95 if (RT_SUCCESS(rc))
96 {
97 rc = RTHeapSimpleInit(&g_HeapExec, pvMemory, cb);
98 if (RT_FAILURE(rc))
99 rtR0MemExecCleanup();
100 }
101 return rc;
102}
103#endif /* RTMEMALLOC_EXEC_HEAP */
104
105
106
107/**
108 * OS specific allocation function.
109 */
110PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
111{
112 /*
113 * Allocate.
114 */
115 PRTMEMHDR pHdr;
116 Assert(cb != sizeof(void *)); /* 99% of pointer sized allocations are wrong. */
117 if (fFlags & RTMEMHDR_FLAG_EXEC)
118 {
119#if defined(RT_ARCH_AMD64)
120# ifdef RTMEMALLOC_EXEC_HEAP
121 if (g_HeapExec != NIL_RTHEAPSIMPLE)
122 {
123 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
124 RTSpinlockAcquireNoInts(g_HeapExecSpinlock, &SpinlockTmp);
125 pHdr = (PRTMEMHDR)RTHeapSimpleAlloc(g_HeapExec, cb + sizeof(*pHdr), 0);
126 RTSpinlockReleaseNoInts(g_HeapExecSpinlock, &SpinlockTmp);
127 fFlags |= RTMEMHDR_FLAG_EXEC_HEAP;
128 }
129 else
130# endif
131 pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
132
133#elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
134 pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM,
135 __pgprot(cpu_has_pge ? _PAGE_KERNEL_EXEC | _PAGE_GLOBAL : _PAGE_KERNEL_EXEC));
136#else
137 pHdr = (PRTMEMHDR)vmalloc(cb + sizeof(*pHdr));
138#endif
139 }
140 else
141 {
142 if (cb <= PAGE_SIZE)
143 {
144 fFlags |= RTMEMHDR_FLAG_KMALLOC;
145 pHdr = kmalloc(cb + sizeof(*pHdr), GFP_KERNEL);
146 }
147 else
148 pHdr = vmalloc(cb + sizeof(*pHdr));
149 }
150
151 /*
152 * Initialize.
153 */
154 if (pHdr)
155 {
156 pHdr->u32Magic = RTMEMHDR_MAGIC;
157 pHdr->fFlags = fFlags;
158 pHdr->cb = cb;
159 pHdr->u32Padding= 0;
160 }
161 return pHdr;
162}
163
164
165/**
166 * OS specific free function.
167 */
168void rtMemFree(PRTMEMHDR pHdr)
169{
170 pHdr->u32Magic += 1;
171 if (pHdr->fFlags & RTMEMHDR_FLAG_KMALLOC)
172 kfree(pHdr);
173#ifdef RTMEMALLOC_EXEC_HEAP
174 else if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC_HEAP)
175 {
176 RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
177 RTSpinlockAcquireNoInts(g_HeapExecSpinlock, &SpinlockTmp);
178 RTHeapSimpleFree(g_HeapExec, pHdr);
179 RTSpinlockReleaseNoInts(g_HeapExecSpinlock, &SpinlockTmp);
180 }
181#endif
182 else
183 vfree(pHdr);
184}
185
186
187/**
188 * Compute order. Some functions allocate 2^order pages.
189 *
190 * @returns order.
191 * @param cPages Number of pages.
192 */
193static int CalcPowerOf2Order(unsigned long cPages)
194{
195 int iOrder;
196 unsigned long cTmp;
197
198 for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
199 ;
200 if (cPages & ~(1 << iOrder))
201 ++iOrder;
202
203 return iOrder;
204}
205
206
207/**
208 * Allocates physical contiguous memory (below 4GB).
209 * The allocation is page aligned and the content is undefined.
210 *
211 * @returns Pointer to the memory block. This is page aligned.
212 * @param pPhys Where to store the physical address.
213 * @param cb The allocation size in bytes. This is always
214 * rounded up to PAGE_SIZE.
215 */
216RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
217{
218 int cOrder;
219 unsigned cPages;
220 struct page *paPages;
221
222 /*
223 * validate input.
224 */
225 Assert(VALID_PTR(pPhys));
226 Assert(cb > 0);
227
228 /*
229 * Allocate page pointer array.
230 */
231 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
232 cPages = cb >> PAGE_SHIFT;
233 cOrder = CalcPowerOf2Order(cPages);
234#ifdef RT_ARCH_AMD64 /** @todo check out if there is a correct way of getting memory below 4GB (physically). */
235 paPages = alloc_pages(GFP_DMA, cOrder);
236#else
237 paPages = alloc_pages(GFP_USER, cOrder);
238#endif
239 if (paPages)
240 {
241 /*
242 * Reserve the pages and mark them executable.
243 */
244 unsigned iPage;
245 for (iPage = 0; iPage < cPages; iPage++)
246 {
247 Assert(!PageHighMem(&paPages[iPage]));
248 if (iPage + 1 < cPages)
249 {
250 AssertMsg( (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage])) + PAGE_SIZE
251 == (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage + 1]))
252 && page_to_phys(&paPages[iPage]) + PAGE_SIZE
253 == page_to_phys(&paPages[iPage + 1]),
254 ("iPage=%i cPages=%u [0]=%#llx,%p [1]=%#llx,%p\n", iPage, cPages,
255 (long long)page_to_phys(&paPages[iPage]), phys_to_virt(page_to_phys(&paPages[iPage])),
256 (long long)page_to_phys(&paPages[iPage + 1]), phys_to_virt(page_to_phys(&paPages[iPage + 1])) ));
257 }
258
259 SetPageReserved(&paPages[iPage]);
260#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 20) /** @todo find the exact kernel where change_page_attr was introduced. */
261 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
262 MY_CHANGE_PAGE_ATTR(&paPages[iPage], 1, MY_PAGE_KERNEL_EXEC);
263#endif
264 }
265 *pPhys = page_to_phys(paPages);
266 return phys_to_virt(page_to_phys(paPages));
267 }
268
269 return NULL;
270}
271
272
273/**
274 * Frees memory allocated ysing RTMemContAlloc().
275 *
276 * @param pv Pointer to return from RTMemContAlloc().
277 * @param cb The cb parameter passed to RTMemContAlloc().
278 */
279RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
280{
281 if (pv)
282 {
283 int cOrder;
284 unsigned cPages;
285 unsigned iPage;
286 struct page *paPages;
287
288 /* validate */
289 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
290 Assert(cb > 0);
291
292 /* calc order and get pages */
293 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
294 cPages = cb >> PAGE_SHIFT;
295 cOrder = CalcPowerOf2Order(cPages);
296 paPages = virt_to_page(pv);
297
298 /*
299 * Restore page attributes freeing the pages.
300 */
301 for (iPage = 0; iPage < cPages; iPage++)
302 {
303 ClearPageReserved(&paPages[iPage]);
304#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 20) /** @todo find the exact kernel where change_page_attr was introduced. */
305 if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
306 MY_CHANGE_PAGE_ATTR(&paPages[iPage], 1, PAGE_KERNEL);
307#endif
308 }
309 __free_pages(paPages, cOrder);
310 }
311}
312
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette