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