VirtualBox

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

Last change on this file since 97872 was 97872, checked in by vboxsync, 2 years ago

IPRT/alloc-r0drv-linux.c: nits

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.4 KB
Line 
1/* $Id: alloc-r0drv-linux.c 97872 2022-12-27 18:26:41Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "the-linux-kernel.h"
42#include "internal/iprt.h"
43#include <iprt/mem.h>
44
45#include <iprt/assert.h>
46#include <iprt/errcore.h>
47#include "r0drv/alloc-r0drv.h"
48
49
50#if (defined(RT_ARCH_AMD64) || defined(DOXYGEN_RUNNING)) && !defined(RTMEMALLOC_EXEC_HEAP)
51# if RTLNX_VER_MIN(2,6,23) && RTLNX_VER_MAX(5,8,0) && !RTLNX_RHEL_MAJ_PREREQ(8,5)
52/**
53 * Starting with 2.6.23 we can use __get_vm_area and map_vm_area to allocate
54 * memory in the moduel range. This is preferrable to the exec heap below.
55 */
56# define RTMEMALLOC_EXEC_VM_AREA
57# else
58/**
59 * We need memory in the module range (~2GB to ~0) this can only be obtained
60 * thru APIs that are not exported (see module_alloc()).
61 *
62 * So, we'll have to create a quick and dirty heap here using BSS memory.
63 * Very annoying and it's going to restrict us!
64 */
65# define RTMEMALLOC_EXEC_HEAP
66# endif
67#endif
68
69#ifdef RTMEMALLOC_EXEC_HEAP
70# include <iprt/heap.h>
71# include <iprt/spinlock.h>
72# include <iprt/errcore.h>
73#endif
74
75#include "internal/initterm.h"
76
77
78/*********************************************************************************************************************************
79* Structures and Typedefs *
80*********************************************************************************************************************************/
81#ifdef RTMEMALLOC_EXEC_VM_AREA
82/**
83 * Extended header used for headers marked with RTMEMHDR_FLAG_EXEC_VM_AREA.
84 *
85 * This is used with allocating executable memory, for things like generated
86 * code and loaded modules.
87 */
88typedef struct RTMEMLNXHDREX
89{
90 /** The VM area for this allocation. */
91 struct vm_struct *pVmArea;
92 void *pvDummy;
93 /** The header we present to the generic API. */
94 RTMEMHDR Hdr;
95} RTMEMLNXHDREX;
96AssertCompileSize(RTMEMLNXHDREX, 32);
97/** Pointer to an extended memory header. */
98typedef RTMEMLNXHDREX *PRTMEMLNXHDREX;
99#endif
100
101
102/*********************************************************************************************************************************
103* Global Variables *
104*********************************************************************************************************************************/
105#ifdef RTMEMALLOC_EXEC_HEAP
106/** The heap. */
107static RTHEAPSIMPLE g_HeapExec = NIL_RTHEAPSIMPLE;
108/** Spinlock protecting the heap. */
109static RTSPINLOCK g_HeapExecSpinlock = NIL_RTSPINLOCK;
110#endif
111
112
113/**
114 * API for cleaning up the heap spinlock on IPRT termination.
115 * This is as RTMemExecDonate specific to AMD64 Linux/GNU.
116 */
117DECLHIDDEN(void) rtR0MemExecCleanup(void)
118{
119#ifdef RTMEMALLOC_EXEC_HEAP
120 RTSpinlockDestroy(g_HeapExecSpinlock);
121 g_HeapExecSpinlock = NIL_RTSPINLOCK;
122#endif
123}
124
125
126/**
127 * Donate read+write+execute memory to the exec heap.
128 *
129 * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
130 * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
131 * allocated memory in the module if it wishes for GCC generated code to work.
132 * GCC can only generate modules that work in the address range ~2GB to ~0
133 * currently.
134 *
135 * The API only accept one single donation.
136 *
137 * @returns IPRT status code.
138 * @retval VERR_NOT_SUPPORTED if the code isn't enabled.
139 * @param pvMemory Pointer to the memory block.
140 * @param cb The size of the memory block.
141 */
142RTR0DECL(int) RTR0MemExecDonate(void *pvMemory, size_t cb)
143{
144#ifdef RTMEMALLOC_EXEC_HEAP
145 int rc;
146 AssertReturn(g_HeapExec == NIL_RTHEAPSIMPLE, VERR_WRONG_ORDER);
147
148 rc = RTSpinlockCreate(&g_HeapExecSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "RTR0MemExecDonate");
149 if (RT_SUCCESS(rc))
150 {
151 rc = RTHeapSimpleInit(&g_HeapExec, pvMemory, cb);
152 if (RT_FAILURE(rc))
153 rtR0MemExecCleanup();
154 }
155 return rc;
156#else
157 RT_NOREF_PV(pvMemory); RT_NOREF_PV(cb);
158 return VERR_NOT_SUPPORTED;
159#endif
160}
161RT_EXPORT_SYMBOL(RTR0MemExecDonate);
162
163
164
165#ifdef RTMEMALLOC_EXEC_VM_AREA
166/**
167 * Allocate executable kernel memory in the module range.
168 *
169 * @returns Pointer to a allocation header success. NULL on failure.
170 *
171 * @param cb The size the user requested.
172 */
173static PRTMEMHDR rtR0MemAllocExecVmArea(size_t cb)
174{
175 size_t const cbAlloc = RT_ALIGN_Z(sizeof(RTMEMLNXHDREX) + cb, PAGE_SIZE);
176 size_t const cPages = cbAlloc >> PAGE_SHIFT;
177 struct page **papPages;
178 struct vm_struct *pVmArea;
179 size_t iPage;
180
181 pVmArea = __get_vm_area(cbAlloc, VM_ALLOC, MODULES_VADDR, MODULES_END);
182 if (!pVmArea)
183 return NULL;
184 pVmArea->nr_pages = 0; /* paranoia? */
185 pVmArea->pages = NULL; /* paranoia? */
186
187 papPages = (struct page **)kmalloc(cPages * sizeof(papPages[0]), GFP_KERNEL | __GFP_NOWARN);
188 if (!papPages)
189 {
190 vunmap(pVmArea->addr);
191 return NULL;
192 }
193
194 for (iPage = 0; iPage < cPages; iPage++)
195 {
196 papPages[iPage] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN);
197 if (!papPages[iPage])
198 break;
199 }
200 if (iPage == cPages)
201 {
202 /*
203 * Map the pages.
204 *
205 * Not entirely sure we really need to set nr_pages and pages here, but
206 * they provide a very convenient place for storing something we need
207 * in the free function, if nothing else...
208 */
209# if RTLNX_VER_MAX(3,17,0)
210 struct page **papPagesIterator = papPages;
211# endif
212 pVmArea->nr_pages = cPages;
213 pVmArea->pages = papPages;
214 if (!map_vm_area(pVmArea, PAGE_KERNEL_EXEC,
215# if RTLNX_VER_MAX(3,17,0)
216 &papPagesIterator
217# else
218 papPages
219# endif
220 ))
221 {
222 PRTMEMLNXHDREX pHdrEx = (PRTMEMLNXHDREX)pVmArea->addr;
223 pHdrEx->pVmArea = pVmArea;
224 pHdrEx->pvDummy = NULL;
225 return &pHdrEx->Hdr;
226 }
227 /* bail out */
228# if RTLNX_VER_MAX(3,17,0)
229 pVmArea->nr_pages = papPagesIterator - papPages;
230# endif
231 }
232
233 vunmap(pVmArea->addr);
234
235 while (iPage-- > 0)
236 __free_page(papPages[iPage]);
237 kfree(papPages);
238
239 return NULL;
240}
241#endif /* RTMEMALLOC_EXEC_VM_AREA */
242
243
244/**
245 * OS specific allocation function.
246 */
247DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
248{
249 PRTMEMHDR pHdr;
250 IPRT_LINUX_SAVE_EFL_AC();
251
252 /*
253 * Allocate.
254 */
255 if (fFlags & RTMEMHDR_FLAG_EXEC)
256 {
257 if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
258 return VERR_NOT_SUPPORTED;
259
260#if defined(RT_ARCH_AMD64)
261# ifdef RTMEMALLOC_EXEC_HEAP
262 if (g_HeapExec != NIL_RTHEAPSIMPLE)
263 {
264 RTSpinlockAcquire(g_HeapExecSpinlock);
265 pHdr = (PRTMEMHDR)RTHeapSimpleAlloc(g_HeapExec, cb + sizeof(*pHdr), 0);
266 RTSpinlockRelease(g_HeapExecSpinlock);
267 fFlags |= RTMEMHDR_FLAG_EXEC_HEAP;
268 }
269 else
270 pHdr = NULL;
271
272# elif defined(RTMEMALLOC_EXEC_VM_AREA)
273 pHdr = rtR0MemAllocExecVmArea(cb);
274 fFlags |= RTMEMHDR_FLAG_EXEC_VM_AREA;
275
276# else /* !RTMEMALLOC_EXEC_HEAP && !RTMEMALLOC_EXEC_VM_AREA */
277# error "you do not want to go here..."
278 pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, MY_PAGE_KERNEL_EXEC);
279# endif /* !RTMEMALLOC_EXEC_HEAP && !RTMEMALLOC_EXEC_VM_AREA */
280
281#elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
282# if RTLNX_VER_MIN(5,8,0)
283 AssertMsgFailed(("This point should not be reached, please file a bug\n"));
284 pHdr = NULL;
285# else
286 pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, MY_PAGE_KERNEL_EXEC);
287# endif
288#else
289 pHdr = (PRTMEMHDR)vmalloc(cb + sizeof(*pHdr));
290#endif
291 }
292 else
293 {
294 if (
295#if 1 /* vmalloc has serious performance issues, avoid it. */
296 cb <= PAGE_SIZE*16 - sizeof(*pHdr)
297#else
298 cb <= PAGE_SIZE
299#endif
300 || (fFlags & RTMEMHDR_FLAG_ANY_CTX)
301 )
302 {
303 fFlags |= RTMEMHDR_FLAG_KMALLOC;
304 pHdr = kmalloc(cb + sizeof(*pHdr),
305 fFlags & RTMEMHDR_FLAG_ANY_CTX_ALLOC ? GFP_ATOMIC | __GFP_NOWARN : GFP_KERNEL | __GFP_NOWARN);
306 if (RT_UNLIKELY( !pHdr
307 && cb > PAGE_SIZE
308 && !(fFlags & RTMEMHDR_FLAG_ANY_CTX) ))
309 {
310 fFlags &= ~RTMEMHDR_FLAG_KMALLOC;
311 pHdr = vmalloc(cb + sizeof(*pHdr));
312 }
313 }
314 else
315 pHdr = vmalloc(cb + sizeof(*pHdr));
316 }
317 if (RT_UNLIKELY(!pHdr))
318 {
319 IPRT_LINUX_RESTORE_EFL_AC();
320 return VERR_NO_MEMORY;
321 }
322
323 /*
324 * Initialize.
325 */
326 pHdr->u32Magic = RTMEMHDR_MAGIC;
327 pHdr->fFlags = fFlags;
328 pHdr->cb = cb;
329 pHdr->cbReq = cb;
330
331 *ppHdr = pHdr;
332 IPRT_LINUX_RESTORE_EFL_AC();
333 return VINF_SUCCESS;
334}
335
336
337/**
338 * OS specific free function.
339 */
340DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
341{
342 IPRT_LINUX_SAVE_EFL_AC();
343
344 pHdr->u32Magic += 1;
345 if (pHdr->fFlags & RTMEMHDR_FLAG_KMALLOC)
346 kfree(pHdr);
347#ifdef RTMEMALLOC_EXEC_HEAP
348 else if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC_HEAP)
349 {
350 RTSpinlockAcquire(g_HeapExecSpinlock);
351 RTHeapSimpleFree(g_HeapExec, pHdr);
352 RTSpinlockRelease(g_HeapExecSpinlock);
353 }
354#endif
355#ifdef RTMEMALLOC_EXEC_VM_AREA
356 else if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC_VM_AREA)
357 {
358 PRTMEMLNXHDREX pHdrEx = RT_FROM_MEMBER(pHdr, RTMEMLNXHDREX, Hdr);
359 size_t iPage = pHdrEx->pVmArea->nr_pages;
360 struct page **papPages = pHdrEx->pVmArea->pages;
361 void *pvMapping = pHdrEx->pVmArea->addr;
362
363 vunmap(pvMapping);
364
365 while (iPage-- > 0)
366 __free_page(papPages[iPage]);
367 kfree(papPages);
368 }
369#endif
370 else
371 vfree(pHdr);
372
373 IPRT_LINUX_RESTORE_EFL_AC();
374}
375
376
377
378/**
379 * Compute order. Some functions allocate 2^order pages.
380 *
381 * @returns order.
382 * @param cPages Number of pages.
383 */
384static int CalcPowerOf2Order(unsigned long cPages)
385{
386 int iOrder;
387 unsigned long cTmp;
388
389 for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
390 ;
391 if (cPages & ~(1 << iOrder))
392 ++iOrder;
393
394 return iOrder;
395}
396
397
398/**
399 * Allocates physical contiguous memory (below 4GB).
400 * The allocation is page aligned and the content is undefined.
401 *
402 * @returns Pointer to the memory block. This is page aligned.
403 * @param pPhys Where to store the physical address.
404 * @param cb The allocation size in bytes. This is always
405 * rounded up to PAGE_SIZE.
406 */
407RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
408{
409 int cOrder;
410 unsigned cPages;
411 struct page *paPages;
412 void *pvRet;
413 IPRT_LINUX_SAVE_EFL_AC();
414
415 /*
416 * validate input.
417 */
418 AssertPtr(pPhys);
419 Assert(cb > 0);
420
421 /*
422 * Allocate page pointer array.
423 */
424 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
425 cPages = cb >> PAGE_SHIFT;
426 cOrder = CalcPowerOf2Order(cPages);
427#if (defined(RT_ARCH_AMD64) || defined(CONFIG_X86_PAE)) && defined(GFP_DMA32)
428 /* ZONE_DMA32: 0-4GB */
429 paPages = alloc_pages(GFP_DMA32 | __GFP_NOWARN, cOrder);
430 if (!paPages)
431#endif
432#ifdef RT_ARCH_AMD64
433 /* ZONE_DMA; 0-16MB */
434 paPages = alloc_pages(GFP_DMA | __GFP_NOWARN, cOrder);
435#else
436 /* ZONE_NORMAL: 0-896MB */
437 paPages = alloc_pages(GFP_USER | __GFP_NOWARN, cOrder);
438#endif
439 if (paPages)
440 {
441 /*
442 * Reserve the pages and mark them executable.
443 */
444 unsigned iPage;
445 for (iPage = 0; iPage < cPages; iPage++)
446 {
447 Assert(!PageHighMem(&paPages[iPage]));
448 if (iPage + 1 < cPages)
449 {
450 AssertMsg( (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage])) + PAGE_SIZE
451 == (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage + 1]))
452 && page_to_phys(&paPages[iPage]) + PAGE_SIZE
453 == page_to_phys(&paPages[iPage + 1]),
454 ("iPage=%i cPages=%u [0]=%#llx,%p [1]=%#llx,%p\n", iPage, cPages,
455 (long long)page_to_phys(&paPages[iPage]), phys_to_virt(page_to_phys(&paPages[iPage])),
456 (long long)page_to_phys(&paPages[iPage + 1]), phys_to_virt(page_to_phys(&paPages[iPage + 1])) ));
457 }
458
459 SetPageReserved(&paPages[iPage]);
460 }
461 *pPhys = page_to_phys(paPages);
462 pvRet = phys_to_virt(page_to_phys(paPages));
463 }
464 else
465 pvRet = NULL;
466
467 IPRT_LINUX_RESTORE_EFL_AC();
468 return pvRet;
469}
470RT_EXPORT_SYMBOL(RTMemContAlloc);
471
472
473/**
474 * Frees memory allocated using RTMemContAlloc().
475 *
476 * @param pv Pointer to return from RTMemContAlloc().
477 * @param cb The cb parameter passed to RTMemContAlloc().
478 */
479RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
480{
481 if (pv)
482 {
483 int cOrder;
484 unsigned cPages;
485 unsigned iPage;
486 struct page *paPages;
487 IPRT_LINUX_SAVE_EFL_AC();
488
489 /* validate */
490 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
491 Assert(cb > 0);
492
493 /* calc order and get pages */
494 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
495 cPages = cb >> PAGE_SHIFT;
496 cOrder = CalcPowerOf2Order(cPages);
497 paPages = virt_to_page(pv);
498
499 /*
500 * Restore page attributes freeing the pages.
501 */
502 for (iPage = 0; iPage < cPages; iPage++)
503 {
504 ClearPageReserved(&paPages[iPage]);
505 }
506 __free_pages(paPages, cOrder);
507 IPRT_LINUX_RESTORE_EFL_AC();
508 }
509}
510RT_EXPORT_SYMBOL(RTMemContFree);
511
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