1 | /* $Id: alloc-r0drv-linux.c 25642 2010-01-05 08:19:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-linux-kernel.h"
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include "r0drv/alloc-r0drv.h"
|
---|
41 |
|
---|
42 | #if defined(RT_ARCH_AMD64) || defined(DOXYGEN_RUNNING)
|
---|
43 | /**
|
---|
44 | * We need memory in the module range (~2GB to ~0) this can only be obtained
|
---|
45 | * thru APIs that are not exported (see module_alloc()).
|
---|
46 | *
|
---|
47 | * So, we'll have to create a quick and dirty heap here using BSS memory.
|
---|
48 | * Very annoying and it's going to restrict us!
|
---|
49 | */
|
---|
50 | # define RTMEMALLOC_EXEC_HEAP
|
---|
51 | #endif
|
---|
52 | #ifdef RTMEMALLOC_EXEC_HEAP
|
---|
53 | # include <iprt/heap.h>
|
---|
54 | # include <iprt/spinlock.h>
|
---|
55 | # include <iprt/err.h>
|
---|
56 | #endif
|
---|
57 |
|
---|
58 |
|
---|
59 | /*******************************************************************************
|
---|
60 | * Global Variables *
|
---|
61 | *******************************************************************************/
|
---|
62 | #ifdef RTMEMALLOC_EXEC_HEAP
|
---|
63 | /** The heap. */
|
---|
64 | static RTHEAPSIMPLE g_HeapExec = NIL_RTHEAPSIMPLE;
|
---|
65 | /** Spinlock protecting the heap. */
|
---|
66 | static RTSPINLOCK g_HeapExecSpinlock = NIL_RTSPINLOCK;
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * API for cleaning up the heap spinlock on IPRT termination.
|
---|
71 | * This is as RTMemExecDonate specific to AMD64 Linux/GNU.
|
---|
72 | */
|
---|
73 | void rtR0MemExecCleanup(void)
|
---|
74 | {
|
---|
75 | RTSpinlockDestroy(g_HeapExecSpinlock);
|
---|
76 | g_HeapExecSpinlock = NIL_RTSPINLOCK;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Donate read+write+execute memory to the exec heap.
|
---|
82 | *
|
---|
83 | * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
|
---|
84 | * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
|
---|
85 | * allocated memory in the module if it wishes for GCC generated code to work.
|
---|
86 | * GCC can only generate modules that work in the address range ~2GB to ~0
|
---|
87 | * currently.
|
---|
88 | *
|
---|
89 | * The API only accept one single donation.
|
---|
90 | *
|
---|
91 | * @returns IPRT status code.
|
---|
92 | * @param pvMemory Pointer to the memory block.
|
---|
93 | * @param cb The size of the memory block.
|
---|
94 | */
|
---|
95 | RTR0DECL(int) RTR0MemExecDonate(void *pvMemory, size_t cb)
|
---|
96 | {
|
---|
97 | int rc;
|
---|
98 | AssertReturn(g_HeapExec == NIL_RTHEAPSIMPLE, VERR_WRONG_ORDER);
|
---|
99 |
|
---|
100 | rc = RTSpinlockCreate(&g_HeapExecSpinlock);
|
---|
101 | if (RT_SUCCESS(rc))
|
---|
102 | {
|
---|
103 | rc = RTHeapSimpleInit(&g_HeapExec, pvMemory, cb);
|
---|
104 | if (RT_FAILURE(rc))
|
---|
105 | rtR0MemExecCleanup();
|
---|
106 | }
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 | RT_EXPORT_SYMBOL(RTR0MemExecDonate);
|
---|
110 | #endif /* RTMEMALLOC_EXEC_HEAP */
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * OS specific allocation function.
|
---|
116 | */
|
---|
117 | PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags)
|
---|
118 | {
|
---|
119 | /*
|
---|
120 | * Allocate.
|
---|
121 | */
|
---|
122 | PRTMEMHDR pHdr;
|
---|
123 | if (fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
124 | {
|
---|
125 | #if defined(RT_ARCH_AMD64)
|
---|
126 | # ifdef RTMEMALLOC_EXEC_HEAP
|
---|
127 | if (g_HeapExec != NIL_RTHEAPSIMPLE)
|
---|
128 | {
|
---|
129 | RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
130 | RTSpinlockAcquireNoInts(g_HeapExecSpinlock, &SpinlockTmp);
|
---|
131 | pHdr = (PRTMEMHDR)RTHeapSimpleAlloc(g_HeapExec, cb + sizeof(*pHdr), 0);
|
---|
132 | RTSpinlockReleaseNoInts(g_HeapExecSpinlock, &SpinlockTmp);
|
---|
133 | fFlags |= RTMEMHDR_FLAG_EXEC_HEAP;
|
---|
134 | }
|
---|
135 | else
|
---|
136 | pHdr = NULL;
|
---|
137 | # else /* !RTMEMALLOC_EXEC_HEAP */
|
---|
138 | pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM, MY_PAGE_KERNEL_EXEC);
|
---|
139 | # endif /* !RTMEMALLOC_EXEC_HEAP */
|
---|
140 |
|
---|
141 | #elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
|
---|
142 | pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM, MY_PAGE_KERNEL_EXEC);
|
---|
143 | #else
|
---|
144 | pHdr = (PRTMEMHDR)vmalloc(cb + sizeof(*pHdr));
|
---|
145 | #endif
|
---|
146 | }
|
---|
147 | else
|
---|
148 | {
|
---|
149 | if (cb <= PAGE_SIZE)
|
---|
150 | {
|
---|
151 | fFlags |= RTMEMHDR_FLAG_KMALLOC;
|
---|
152 | pHdr = kmalloc(cb + sizeof(*pHdr), GFP_KERNEL);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | pHdr = vmalloc(cb + sizeof(*pHdr));
|
---|
156 | }
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Initialize.
|
---|
160 | */
|
---|
161 | if (pHdr)
|
---|
162 | {
|
---|
163 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
164 | pHdr->fFlags = fFlags;
|
---|
165 | pHdr->cb = cb;
|
---|
166 | pHdr->cbReq = cb;
|
---|
167 | }
|
---|
168 | return pHdr;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * OS specific free function.
|
---|
174 | */
|
---|
175 | void rtMemFree(PRTMEMHDR pHdr)
|
---|
176 | {
|
---|
177 | pHdr->u32Magic += 1;
|
---|
178 | if (pHdr->fFlags & RTMEMHDR_FLAG_KMALLOC)
|
---|
179 | kfree(pHdr);
|
---|
180 | #ifdef RTMEMALLOC_EXEC_HEAP
|
---|
181 | else if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC_HEAP)
|
---|
182 | {
|
---|
183 | RTSPINLOCKTMP SpinlockTmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
184 | RTSpinlockAcquireNoInts(g_HeapExecSpinlock, &SpinlockTmp);
|
---|
185 | RTHeapSimpleFree(g_HeapExec, pHdr);
|
---|
186 | RTSpinlockReleaseNoInts(g_HeapExecSpinlock, &SpinlockTmp);
|
---|
187 | }
|
---|
188 | #endif
|
---|
189 | else
|
---|
190 | vfree(pHdr);
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Compute order. Some functions allocate 2^order pages.
|
---|
196 | *
|
---|
197 | * @returns order.
|
---|
198 | * @param cPages Number of pages.
|
---|
199 | */
|
---|
200 | static int CalcPowerOf2Order(unsigned long cPages)
|
---|
201 | {
|
---|
202 | int iOrder;
|
---|
203 | unsigned long cTmp;
|
---|
204 |
|
---|
205 | for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
|
---|
206 | ;
|
---|
207 | if (cPages & ~(1 << iOrder))
|
---|
208 | ++iOrder;
|
---|
209 |
|
---|
210 | return iOrder;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Allocates physical contiguous memory (below 4GB).
|
---|
216 | * The allocation is page aligned and the content is undefined.
|
---|
217 | *
|
---|
218 | * @returns Pointer to the memory block. This is page aligned.
|
---|
219 | * @param pPhys Where to store the physical address.
|
---|
220 | * @param cb The allocation size in bytes. This is always
|
---|
221 | * rounded up to PAGE_SIZE.
|
---|
222 | */
|
---|
223 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
224 | {
|
---|
225 | int cOrder;
|
---|
226 | unsigned cPages;
|
---|
227 | struct page *paPages;
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * validate input.
|
---|
231 | */
|
---|
232 | Assert(VALID_PTR(pPhys));
|
---|
233 | Assert(cb > 0);
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * Allocate page pointer array.
|
---|
237 | */
|
---|
238 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
239 | cPages = cb >> PAGE_SHIFT;
|
---|
240 | cOrder = CalcPowerOf2Order(cPages);
|
---|
241 | #if (defined(RT_ARCH_AMD64) || defined(CONFIG_X86_PAE)) && defined(GFP_DMA32)
|
---|
242 | /* ZONE_DMA32: 0-4GB */
|
---|
243 | paPages = alloc_pages(GFP_DMA32, cOrder);
|
---|
244 | if (!paPages)
|
---|
245 | #endif
|
---|
246 | #ifdef RT_ARCH_AMD64
|
---|
247 | /* ZONE_DMA; 0-16MB */
|
---|
248 | paPages = alloc_pages(GFP_DMA, cOrder);
|
---|
249 | #else
|
---|
250 | /* ZONE_NORMAL: 0-896MB */
|
---|
251 | paPages = alloc_pages(GFP_USER, cOrder);
|
---|
252 | #endif
|
---|
253 | if (paPages)
|
---|
254 | {
|
---|
255 | /*
|
---|
256 | * Reserve the pages and mark them executable.
|
---|
257 | */
|
---|
258 | unsigned iPage;
|
---|
259 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
260 | {
|
---|
261 | Assert(!PageHighMem(&paPages[iPage]));
|
---|
262 | if (iPage + 1 < cPages)
|
---|
263 | {
|
---|
264 | AssertMsg( (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage])) + PAGE_SIZE
|
---|
265 | == (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage + 1]))
|
---|
266 | && page_to_phys(&paPages[iPage]) + PAGE_SIZE
|
---|
267 | == page_to_phys(&paPages[iPage + 1]),
|
---|
268 | ("iPage=%i cPages=%u [0]=%#llx,%p [1]=%#llx,%p\n", iPage, cPages,
|
---|
269 | (long long)page_to_phys(&paPages[iPage]), phys_to_virt(page_to_phys(&paPages[iPage])),
|
---|
270 | (long long)page_to_phys(&paPages[iPage + 1]), phys_to_virt(page_to_phys(&paPages[iPage + 1])) ));
|
---|
271 | }
|
---|
272 |
|
---|
273 | SetPageReserved(&paPages[iPage]);
|
---|
274 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 20) /** @todo find the exact kernel where change_page_attr was introduced. */
|
---|
275 | MY_SET_PAGES_EXEC(&paPages[iPage], 1);
|
---|
276 | #endif
|
---|
277 | }
|
---|
278 | *pPhys = page_to_phys(paPages);
|
---|
279 | return phys_to_virt(page_to_phys(paPages));
|
---|
280 | }
|
---|
281 |
|
---|
282 | return NULL;
|
---|
283 | }
|
---|
284 | RT_EXPORT_SYMBOL(RTMemContAlloc);
|
---|
285 |
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Frees memory allocated ysing RTMemContAlloc().
|
---|
289 | *
|
---|
290 | * @param pv Pointer to return from RTMemContAlloc().
|
---|
291 | * @param cb The cb parameter passed to RTMemContAlloc().
|
---|
292 | */
|
---|
293 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
294 | {
|
---|
295 | if (pv)
|
---|
296 | {
|
---|
297 | int cOrder;
|
---|
298 | unsigned cPages;
|
---|
299 | unsigned iPage;
|
---|
300 | struct page *paPages;
|
---|
301 |
|
---|
302 | /* validate */
|
---|
303 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
304 | Assert(cb > 0);
|
---|
305 |
|
---|
306 | /* calc order and get pages */
|
---|
307 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
308 | cPages = cb >> PAGE_SHIFT;
|
---|
309 | cOrder = CalcPowerOf2Order(cPages);
|
---|
310 | paPages = virt_to_page(pv);
|
---|
311 |
|
---|
312 | /*
|
---|
313 | * Restore page attributes freeing the pages.
|
---|
314 | */
|
---|
315 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
316 | {
|
---|
317 | ClearPageReserved(&paPages[iPage]);
|
---|
318 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 4, 20) /** @todo find the exact kernel where change_page_attr was introduced. */
|
---|
319 | MY_SET_PAGES_NOEXEC(&paPages[iPage], 1);
|
---|
320 | #endif
|
---|
321 | }
|
---|
322 | __free_pages(paPages, cOrder);
|
---|
323 | }
|
---|
324 | }
|
---|
325 | RT_EXPORT_SYMBOL(RTMemContFree);
|
---|
326 |
|
---|