VirtualBox

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

Last change on this file since 32707 was 32707, checked in by vboxsync, 15 years ago

IPRT: Added RTMemAllocEx[Tag] and RTMemFreeEx, only implemented in ring-0 only.

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

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