VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/memobj-r0drv-linux.c@ 104986

Last change on this file since 104986 was 104986, checked in by vboxsync, 5 months ago

iprt: Linux: Add initial support for RHEL 9.5 kernel, bugref:10381, bugref:10482.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Revision
File size: 75.2 KB
Line 
1/* $Id: memobj-r0drv-linux.c 104986 2024-06-20 14:47:07Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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
43#include <iprt/memobj.h>
44#include <iprt/assert.h>
45#include <iprt/err.h>
46#include <iprt/log.h>
47#include <iprt/mem.h>
48#include <iprt/process.h>
49#include <iprt/string.h>
50#include "internal/memobj.h"
51#include "internal/iprt.h"
52
53
54/*********************************************************************************************************************************
55* Defined Constants And Macros *
56*********************************************************************************************************************************/
57/* early 2.6 kernels */
58#ifndef PAGE_SHARED_EXEC
59# define PAGE_SHARED_EXEC PAGE_SHARED
60#endif
61#ifndef PAGE_READONLY_EXEC
62# define PAGE_READONLY_EXEC PAGE_READONLY
63#endif
64
65/** @def IPRT_USE_ALLOC_VM_AREA_FOR_EXEC
66 * Whether we use alloc_vm_area (3.2+) for executable memory.
67 * This is a must for 5.8+, but we enable it all the way back to 3.2.x for
68 * better W^R compliance (fExecutable flag). */
69#if RTLNX_VER_RANGE(3,2,0, 5,10,0) || defined(DOXYGEN_RUNNING)
70# define IPRT_USE_ALLOC_VM_AREA_FOR_EXEC
71#endif
72/** @def IPRT_USE_APPLY_TO_PAGE_RANGE_FOR_EXEC
73 * alloc_vm_area was removed with 5.10 so we have to resort to a different way
74 * to allocate executable memory.
75 * It would be possible to remove IPRT_USE_ALLOC_VM_AREA_FOR_EXEC and use
76 * this path execlusively for 3.2+ but no time to test it really works on every
77 * supported kernel, so better play safe for now.
78 */
79#if RTLNX_VER_MIN(5,10,0) || defined(DOXYGEN_RUNNING)
80# define IPRT_USE_APPLY_TO_PAGE_RANGE_FOR_EXEC
81#endif
82
83/*
84 * 2.6.29+ kernels don't work with remap_pfn_range() anymore because
85 * track_pfn_vma_new() is apparently not defined for non-RAM pages.
86 * It should be safe to use vm_insert_page() older kernels as well.
87 */
88#if RTLNX_VER_MIN(2,6,23)
89# define VBOX_USE_INSERT_PAGE
90#endif
91#if defined(CONFIG_X86_PAE) \
92 && ( defined(HAVE_26_STYLE_REMAP_PAGE_RANGE) \
93 || RTLNX_VER_RANGE(2,6,0, 2,6,11) )
94# define VBOX_USE_PAE_HACK
95#endif
96
97/* gfp_t was introduced in 2.6.14, define it for earlier. */
98#if RTLNX_VER_MAX(2,6,14)
99# define gfp_t unsigned
100#endif
101
102/*
103 * Wrappers around mmap_lock/mmap_sem difference.
104 */
105#if RTLNX_VER_MIN(5,8,0)
106# define LNX_MM_DOWN_READ(a_pMm) down_read(&(a_pMm)->mmap_lock)
107# define LNX_MM_UP_READ(a_pMm) up_read(&(a_pMm)->mmap_lock)
108# define LNX_MM_DOWN_WRITE(a_pMm) down_write(&(a_pMm)->mmap_lock)
109# define LNX_MM_UP_WRITE(a_pMm) up_write(&(a_pMm)->mmap_lock)
110#else
111# define LNX_MM_DOWN_READ(a_pMm) down_read(&(a_pMm)->mmap_sem)
112# define LNX_MM_UP_READ(a_pMm) up_read(&(a_pMm)->mmap_sem)
113# define LNX_MM_DOWN_WRITE(a_pMm) down_write(&(a_pMm)->mmap_sem)
114# define LNX_MM_UP_WRITE(a_pMm) up_write(&(a_pMm)->mmap_sem)
115#endif
116
117
118/*********************************************************************************************************************************
119* Structures and Typedefs *
120*********************************************************************************************************************************/
121/**
122 * The Linux version of the memory object structure.
123 */
124typedef struct RTR0MEMOBJLNX
125{
126 /** The core structure. */
127 RTR0MEMOBJINTERNAL Core;
128 /** Set if the allocation is contiguous.
129 * This means it has to be given back as one chunk. */
130 bool fContiguous;
131 /** Set if executable allocation. */
132 bool fExecutable;
133 /** Set if we've vmap'ed the memory into ring-0. */
134 bool fMappedToRing0;
135 /** This is non-zero if large page allocation. */
136 uint8_t cLargePageOrder;
137#ifdef IPRT_USE_ALLOC_VM_AREA_FOR_EXEC
138 /** Return from alloc_vm_area() that we now need to use for executable
139 * memory. */
140 struct vm_struct *pArea;
141 /** PTE array that goes along with pArea (must be freed). */
142 pte_t **papPtesForArea;
143#endif
144 /** The pages in the apPages array. */
145 size_t cPages;
146 /** Array of struct page pointers. (variable size) */
147 RT_FLEXIBLE_ARRAY_EXTENSION
148 struct page *apPages[RT_FLEXIBLE_ARRAY];
149} RTR0MEMOBJLNX;
150/** Pointer to the linux memory object. */
151typedef RTR0MEMOBJLNX *PRTR0MEMOBJLNX;
152
153
154/*********************************************************************************************************************************
155* Global Variables *
156*********************************************************************************************************************************/
157/*
158 * Linux allows only a coarse selection of zones for
159 * allocations matching a particular maximum physical address.
160 *
161 * Sorted from high to low physical address!
162 */
163static const struct
164{
165 RTHCPHYS PhysHighest;
166 gfp_t fGfp;
167} g_aZones[] =
168{
169 { NIL_RTHCPHYS, GFP_KERNEL },
170#if (defined(RT_ARCH_AMD64) || defined(CONFIG_X86_PAE)) && defined(GFP_DMA32)
171 { _4G - 1, GFP_DMA32 }, /* ZONE_DMA32: 0-4GB */
172#elif defined(RT_ARCH_ARM32) || defined(RT_ARCH_ARM64)
173 { _4G - 1, GFP_DMA }, /* ZONE_DMA: 0-4GB */
174#endif
175#if defined(RT_ARCH_AMD64)
176 { _16M - 1, GFP_DMA }, /* ZONE_DMA: 0-16MB */
177#elif defined(RT_ARCH_X86)
178 { 896 * _1M - 1, GFP_USER }, /* ZONE_NORMAL (32-bit hosts): 0-896MB */
179#endif
180};
181
182
183static void rtR0MemObjLinuxFreePages(PRTR0MEMOBJLNX pMemLnx);
184
185
186/**
187 * Helper that converts from a RTR0PROCESS handle to a linux task.
188 *
189 * @returns The corresponding Linux task.
190 * @param R0Process IPRT ring-0 process handle.
191 */
192static struct task_struct *rtR0ProcessToLinuxTask(RTR0PROCESS R0Process)
193{
194 /** @todo fix rtR0ProcessToLinuxTask!! */
195 /** @todo many (all?) callers currently assume that we return 'current'! */
196 return R0Process == RTR0ProcHandleSelf() ? current : NULL;
197}
198
199
200/**
201 * Compute order. Some functions allocate 2^order pages.
202 *
203 * @returns order.
204 * @param cPages Number of pages.
205 */
206static int rtR0MemObjLinuxOrder(size_t cPages)
207{
208 int iOrder;
209 size_t cTmp;
210
211 for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
212 ;
213 if (cPages & ~((size_t)1 << iOrder))
214 ++iOrder;
215
216 return iOrder;
217}
218
219
220/**
221 * Converts from RTMEM_PROT_* to Linux PAGE_*.
222 *
223 * @returns Linux page protection constant.
224 * @param fProt The IPRT protection mask.
225 * @param fKernel Whether it applies to kernel or user space.
226 */
227static pgprot_t rtR0MemObjLinuxConvertProt(unsigned fProt, bool fKernel)
228{
229 switch (fProt)
230 {
231 default:
232 AssertMsgFailed(("%#x %d\n", fProt, fKernel)); RT_FALL_THRU();
233 case RTMEM_PROT_NONE:
234 return PAGE_NONE;
235
236 case RTMEM_PROT_READ:
237 return fKernel ? PAGE_KERNEL_RO : PAGE_READONLY;
238
239 case RTMEM_PROT_WRITE:
240 case RTMEM_PROT_WRITE | RTMEM_PROT_READ:
241 return fKernel ? PAGE_KERNEL : PAGE_SHARED;
242
243 case RTMEM_PROT_EXEC:
244 case RTMEM_PROT_EXEC | RTMEM_PROT_READ:
245#if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
246 if (fKernel)
247 {
248# if RTLNX_VER_MIN(6,6,0) || RTLNX_SUSE_MAJ_PREREQ(15, 6)
249 /* In kernel 6.6 mk_pte() macro was fortified with additional
250 * check which does not allow to use our custom mask anymore
251 * (see kernel commit ae1f05a617dcbc0a732fbeba0893786cd009536c).
252 * For this particular mapping case, an existing mask PAGE_KERNEL_ROX
253 * can be used instead. PAGE_KERNEL_ROX was introduced in
254 * kernel 5.8, however, lets apply it for kernels 6.6 and newer
255 * to be on a safe side.
256 */
257 return PAGE_KERNEL_ROX;
258# else
259 pgprot_t fPg = MY_PAGE_KERNEL_EXEC;
260 pgprot_val(fPg) &= ~_PAGE_RW;
261 return fPg;
262# endif
263 }
264 return PAGE_READONLY_EXEC;
265#else
266 return fKernel ? MY_PAGE_KERNEL_EXEC : PAGE_READONLY_EXEC;
267#endif
268
269 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
270 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_READ:
271 return fKernel ? MY_PAGE_KERNEL_EXEC : PAGE_SHARED_EXEC;
272 }
273}
274
275
276/**
277 * Worker for rtR0MemObjNativeReserveUser and rtR0MemObjNativerMapUser that creates
278 * an empty user space mapping.
279 *
280 * We acquire the mmap_sem/mmap_lock of the task!
281 *
282 * @returns Pointer to the mapping.
283 * (void *)-1 on failure.
284 * @param R3PtrFixed (RTR3PTR)-1 if anywhere, otherwise a specific location.
285 * @param cb The size of the mapping.
286 * @param uAlignment The alignment of the mapping.
287 * @param pTask The Linux task to create this mapping in.
288 * @param fProt The RTMEM_PROT_* mask.
289 */
290static void *rtR0MemObjLinuxDoMmap(RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, struct task_struct *pTask, unsigned fProt)
291{
292 unsigned fLnxProt;
293 unsigned long ulAddr;
294
295 Assert(pTask == current); /* do_mmap */
296 RT_NOREF_PV(pTask);
297
298 /*
299 * Convert from IPRT protection to mman.h PROT_ and call do_mmap.
300 */
301 fProt &= (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
302 if (fProt == RTMEM_PROT_NONE)
303 fLnxProt = PROT_NONE;
304 else
305 {
306 fLnxProt = 0;
307 if (fProt & RTMEM_PROT_READ)
308 fLnxProt |= PROT_READ;
309 if (fProt & RTMEM_PROT_WRITE)
310 fLnxProt |= PROT_WRITE;
311 if (fProt & RTMEM_PROT_EXEC)
312 fLnxProt |= PROT_EXEC;
313 }
314
315 if (R3PtrFixed != (RTR3PTR)-1)
316 {
317#if RTLNX_VER_MIN(3,5,0)
318 ulAddr = vm_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
319#else
320 LNX_MM_DOWN_WRITE(pTask->mm);
321 ulAddr = do_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
322 LNX_MM_UP_WRITE(pTask->mm);
323#endif
324 }
325 else
326 {
327#if RTLNX_VER_MIN(3,5,0)
328 ulAddr = vm_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
329#else
330 LNX_MM_DOWN_WRITE(pTask->mm);
331 ulAddr = do_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
332 LNX_MM_UP_WRITE(pTask->mm);
333#endif
334 if ( !(ulAddr & ~PAGE_MASK)
335 && (ulAddr & (uAlignment - 1)))
336 {
337 /** @todo implement uAlignment properly... We'll probably need to make some dummy mappings to fill
338 * up alignment gaps. This is of course complicated by fragmentation (which we might have cause
339 * ourselves) and further by there begin two mmap strategies (top / bottom). */
340 /* For now, just ignore uAlignment requirements... */
341 }
342 }
343
344
345 if (ulAddr & ~PAGE_MASK) /* ~PAGE_MASK == PAGE_OFFSET_MASK */
346 return (void *)-1;
347 return (void *)ulAddr;
348}
349
350
351/**
352 * Worker that destroys a user space mapping.
353 * Undoes what rtR0MemObjLinuxDoMmap did.
354 *
355 * We acquire the mmap_sem/mmap_lock of the task!
356 *
357 * @param pv The ring-3 mapping.
358 * @param cb The size of the mapping.
359 * @param pTask The Linux task to destroy this mapping in.
360 */
361static void rtR0MemObjLinuxDoMunmap(void *pv, size_t cb, struct task_struct *pTask)
362{
363#if RTLNX_VER_MIN(3,5,0)
364 Assert(pTask == current); RT_NOREF_PV(pTask);
365 vm_munmap((unsigned long)pv, cb);
366#elif defined(USE_RHEL4_MUNMAP)
367 LNX_MM_DOWN_WRITE(pTask->mm);
368 do_munmap(pTask->mm, (unsigned long)pv, cb, 0); /* should it be 1 or 0? */
369 LNX_MM_UP_WRITE(pTask->mm);
370#else
371 LNX_MM_DOWN_WRITE(pTask->mm);
372 do_munmap(pTask->mm, (unsigned long)pv, cb);
373 LNX_MM_UP_WRITE(pTask->mm);
374#endif
375}
376
377
378/**
379 * Internal worker that allocates physical pages and creates the memory object for them.
380 *
381 * @returns IPRT status code.
382 * @param ppMemLnx Where to store the memory object pointer.
383 * @param enmType The object type.
384 * @param cb The number of bytes to allocate.
385 * @param uAlignment The alignment of the physical memory.
386 * Only valid if fContiguous == true, ignored otherwise.
387 * @param fFlagsLnx The page allocation flags (GPFs).
388 * @param fContiguous Whether the allocation must be contiguous.
389 * @param fExecutable Whether the memory must be executable.
390 * @param rcNoMem What to return when we're out of pages.
391 * @param pszTag Allocation tag used for statistics and such.
392 */
393static int rtR0MemObjLinuxAllocPages(PRTR0MEMOBJLNX *ppMemLnx, RTR0MEMOBJTYPE enmType, size_t cb,
394 size_t uAlignment, gfp_t fFlagsLnx, bool fContiguous, bool fExecutable, int rcNoMem,
395 const char *pszTag)
396{
397 size_t iPage;
398 size_t const cPages = cb >> PAGE_SHIFT;
399 struct page *paPages;
400
401 /*
402 * Allocate a memory object structure that's large enough to contain
403 * the page pointer array.
404 */
405 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJLNX, apPages[cPages]), enmType,
406 NULL, cb, pszTag);
407 if (!pMemLnx)
408 return VERR_NO_MEMORY;
409 pMemLnx->Core.fFlags |= RTR0MEMOBJ_FLAGS_UNINITIALIZED_AT_ALLOC;
410 pMemLnx->cPages = cPages;
411
412 if (cPages > 255)
413 {
414# ifdef __GFP_REPEAT
415 /* Try hard to allocate the memory, but the allocation attempt might fail. */
416 fFlagsLnx |= __GFP_REPEAT;
417# endif
418# ifdef __GFP_NOMEMALLOC
419 /* Introduced with Linux 2.6.12: Don't use emergency reserves */
420 fFlagsLnx |= __GFP_NOMEMALLOC;
421# endif
422 }
423
424 /*
425 * Allocate the pages.
426 * For small allocations we'll try contiguous first and then fall back on page by page.
427 */
428#if RTLNX_VER_MIN(2,4,22)
429 if ( fContiguous
430 || cb <= PAGE_SIZE * 2)
431 {
432# ifdef VBOX_USE_INSERT_PAGE
433 paPages = alloc_pages(fFlagsLnx | __GFP_COMP | __GFP_NOWARN, rtR0MemObjLinuxOrder(cPages));
434# else
435 paPages = alloc_pages(fFlagsLnx | __GFP_NOWARN, rtR0MemObjLinuxOrder(cPages));
436# endif
437 if (paPages)
438 {
439 fContiguous = true;
440 for (iPage = 0; iPage < cPages; iPage++)
441 pMemLnx->apPages[iPage] = &paPages[iPage];
442 }
443 else if (fContiguous)
444 {
445 rtR0MemObjDelete(&pMemLnx->Core);
446 return rcNoMem;
447 }
448 }
449
450 if (!fContiguous)
451 {
452 /** @todo Try use alloc_pages_bulk_array when available, it should be faster
453 * than a alloc_page loop. Put it in #ifdefs similar to
454 * IPRT_USE_APPLY_TO_PAGE_RANGE_FOR_EXEC. */
455 for (iPage = 0; iPage < cPages; iPage++)
456 {
457 pMemLnx->apPages[iPage] = alloc_page(fFlagsLnx | __GFP_NOWARN);
458 if (RT_UNLIKELY(!pMemLnx->apPages[iPage]))
459 {
460 while (iPage-- > 0)
461 __free_page(pMemLnx->apPages[iPage]);
462 rtR0MemObjDelete(&pMemLnx->Core);
463 return rcNoMem;
464 }
465 }
466 }
467
468#else /* < 2.4.22 */
469 /** @todo figure out why we didn't allocate page-by-page on 2.4.21 and older... */
470 paPages = alloc_pages(fFlagsLnx, rtR0MemObjLinuxOrder(cPages));
471 if (!paPages)
472 {
473 rtR0MemObjDelete(&pMemLnx->Core);
474 return rcNoMem;
475 }
476 for (iPage = 0; iPage < cPages; iPage++)
477 {
478 pMemLnx->apPages[iPage] = &paPages[iPage];
479 if (fExecutable)
480 MY_SET_PAGES_EXEC(pMemLnx->apPages[iPage], 1);
481 if (PageHighMem(pMemLnx->apPages[iPage]))
482 BUG();
483 }
484
485 fContiguous = true;
486#endif /* < 2.4.22 */
487 pMemLnx->fContiguous = fContiguous;
488 pMemLnx->fExecutable = fExecutable;
489
490#if RTLNX_VER_MAX(4,5,0)
491 /*
492 * Reserve the pages.
493 *
494 * Linux >= 4.5 with CONFIG_DEBUG_VM panics when setting PG_reserved on compound
495 * pages. According to Michal Hocko this shouldn't be necessary anyway because
496 * as pages which are not on the LRU list are never evictable.
497 */
498 for (iPage = 0; iPage < cPages; iPage++)
499 SetPageReserved(pMemLnx->apPages[iPage]);
500#endif
501
502 /*
503 * Note that the physical address of memory allocated with alloc_pages(flags, order)
504 * is always 2^(PAGE_SHIFT+order)-aligned.
505 */
506 if ( fContiguous
507 && uAlignment > PAGE_SIZE)
508 {
509 /*
510 * Check for alignment constraints. The physical address of memory allocated with
511 * alloc_pages(flags, order) is always 2^(PAGE_SHIFT+order)-aligned.
512 */
513 if (RT_UNLIKELY(page_to_phys(pMemLnx->apPages[0]) & (uAlignment - 1)))
514 {
515 /*
516 * This should never happen!
517 */
518 printk("rtR0MemObjLinuxAllocPages(cb=0x%lx, uAlignment=0x%lx): alloc_pages(..., %d) returned physical memory at 0x%lx!\n",
519 (unsigned long)cb, (unsigned long)uAlignment, rtR0MemObjLinuxOrder(cPages), (unsigned long)page_to_phys(pMemLnx->apPages[0]));
520 rtR0MemObjLinuxFreePages(pMemLnx);
521 return rcNoMem;
522 }
523 }
524
525 *ppMemLnx = pMemLnx;
526 return VINF_SUCCESS;
527}
528
529
530/**
531 * Frees the physical pages allocated by the rtR0MemObjLinuxAllocPages() call.
532 *
533 * This method does NOT free the object.
534 *
535 * @param pMemLnx The object which physical pages should be freed.
536 */
537static void rtR0MemObjLinuxFreePages(PRTR0MEMOBJLNX pMemLnx)
538{
539 size_t iPage = pMemLnx->cPages;
540 if (iPage > 0)
541 {
542 /*
543 * Restore the page flags.
544 */
545 while (iPage-- > 0)
546 {
547#if RTLNX_VER_MAX(4,5,0)
548 /* See SetPageReserved() in rtR0MemObjLinuxAllocPages() */
549 ClearPageReserved(pMemLnx->apPages[iPage]);
550#endif
551#if RTLNX_VER_MAX(2,4,22)
552 if (pMemLnx->fExecutable)
553 MY_SET_PAGES_NOEXEC(pMemLnx->apPages[iPage], 1);
554#endif
555 }
556
557 /*
558 * Free the pages.
559 */
560#if RTLNX_VER_MIN(2,4,22)
561 if (!pMemLnx->fContiguous)
562 {
563 iPage = pMemLnx->cPages;
564 while (iPage-- > 0)
565 __free_page(pMemLnx->apPages[iPage]);
566 }
567 else
568#endif
569 __free_pages(pMemLnx->apPages[0], rtR0MemObjLinuxOrder(pMemLnx->cPages));
570
571 pMemLnx->cPages = 0;
572 }
573}
574
575
576#ifdef IPRT_USE_APPLY_TO_PAGE_RANGE_FOR_EXEC
577/**
578 * User data passed to the apply_to_page_range() callback.
579 */
580typedef struct LNXAPPLYPGRANGE
581{
582 /** Pointer to the memory object. */
583 PRTR0MEMOBJLNX pMemLnx;
584 /** The page protection flags to apply. */
585 pgprot_t fPg;
586} LNXAPPLYPGRANGE;
587/** Pointer to the user data. */
588typedef LNXAPPLYPGRANGE *PLNXAPPLYPGRANGE;
589/** Pointer to the const user data. */
590typedef const LNXAPPLYPGRANGE *PCLNXAPPLYPGRANGE;
591
592/**
593 * Callback called in apply_to_page_range().
594 *
595 * @returns Linux status code.
596 * @param pPte Pointer to the page table entry for the given address.
597 * @param uAddr The address to apply the new protection to.
598 * @param pvUser The opaque user data.
599 */
600static int rtR0MemObjLinuxApplyPageRange(pte_t *pPte, unsigned long uAddr, void *pvUser)
601{
602 PCLNXAPPLYPGRANGE pArgs = (PCLNXAPPLYPGRANGE)pvUser;
603 PRTR0MEMOBJLNX pMemLnx = pArgs->pMemLnx;
604 size_t idxPg = (uAddr - (unsigned long)pMemLnx->Core.pv) >> PAGE_SHIFT;
605
606 set_pte(pPte, mk_pte(pMemLnx->apPages[idxPg], pArgs->fPg));
607 return 0;
608}
609#endif
610
611
612/**
613 * Maps the allocation into ring-0.
614 *
615 * This will update the RTR0MEMOBJLNX::Core.pv and RTR0MEMOBJ::fMappedToRing0 members.
616 *
617 * Contiguous mappings that isn't in 'high' memory will already be mapped into kernel
618 * space, so we'll use that mapping if possible. If execute access is required, we'll
619 * play safe and do our own mapping.
620 *
621 * @returns IPRT status code.
622 * @param pMemLnx The linux memory object to map.
623 * @param fExecutable Whether execute access is required.
624 */
625static int rtR0MemObjLinuxVMap(PRTR0MEMOBJLNX pMemLnx, bool fExecutable)
626{
627 int rc = VINF_SUCCESS;
628
629 /*
630 * Choose mapping strategy.
631 */
632 bool fMustMap = fExecutable
633 || !pMemLnx->fContiguous;
634 if (!fMustMap)
635 {
636 size_t iPage = pMemLnx->cPages;
637 while (iPage-- > 0)
638 if (PageHighMem(pMemLnx->apPages[iPage]))
639 {
640 fMustMap = true;
641 break;
642 }
643 }
644
645 Assert(!pMemLnx->Core.pv);
646 Assert(!pMemLnx->fMappedToRing0);
647
648 if (fMustMap)
649 {
650 /*
651 * Use vmap - 2.4.22 and later.
652 */
653#if RTLNX_VER_MIN(2,4,22) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
654 pgprot_t fPg;
655 pgprot_val(fPg) = _PAGE_PRESENT | _PAGE_RW;
656# ifdef _PAGE_NX
657 if (!fExecutable)
658 pgprot_val(fPg) |= _PAGE_NX;
659# endif
660
661# ifdef IPRT_USE_ALLOC_VM_AREA_FOR_EXEC
662 if (fExecutable)
663 {
664# if RTLNX_VER_MIN(3,2,51)
665 pte_t **papPtes = (pte_t **)kmalloc_array(pMemLnx->cPages, sizeof(papPtes[0]), GFP_KERNEL);
666# else
667 pte_t **papPtes = (pte_t **)kmalloc(pMemLnx->cPages * sizeof(papPtes[0]), GFP_KERNEL);
668# endif
669 if (papPtes)
670 {
671 pMemLnx->pArea = alloc_vm_area(pMemLnx->Core.cb, papPtes); /* Note! pArea->nr_pages is not set. */
672 if (pMemLnx->pArea)
673 {
674 size_t i;
675 Assert(pMemLnx->pArea->size >= pMemLnx->Core.cb); /* Note! includes guard page. */
676 Assert(pMemLnx->pArea->addr);
677# ifdef _PAGE_NX
678 pgprot_val(fPg) |= _PAGE_NX; /* Uses RTR0MemObjProtect to clear NX when memory ready, W^X fashion. */
679# endif
680 pMemLnx->papPtesForArea = papPtes;
681 for (i = 0; i < pMemLnx->cPages; i++)
682 *papPtes[i] = mk_pte(pMemLnx->apPages[i], fPg);
683 pMemLnx->Core.pv = pMemLnx->pArea->addr;
684 pMemLnx->fMappedToRing0 = true;
685 }
686 else
687 {
688 kfree(papPtes);
689 rc = VERR_MAP_FAILED;
690 }
691 }
692 else
693 rc = VERR_MAP_FAILED;
694 }
695 else
696# endif
697 {
698# if defined(IPRT_USE_APPLY_TO_PAGE_RANGE_FOR_EXEC)
699 if (fExecutable)
700 pgprot_val(fPg) |= _PAGE_NX; /* Uses RTR0MemObjProtect to clear NX when memory ready, W^X fashion. */
701# endif
702
703# ifdef VM_MAP
704 pMemLnx->Core.pv = vmap(&pMemLnx->apPages[0], pMemLnx->cPages, VM_MAP, fPg);
705# else
706 pMemLnx->Core.pv = vmap(&pMemLnx->apPages[0], pMemLnx->cPages, VM_ALLOC, fPg);
707# endif
708 if (pMemLnx->Core.pv)
709 pMemLnx->fMappedToRing0 = true;
710 else
711 rc = VERR_MAP_FAILED;
712 }
713#else /* < 2.4.22 */
714 rc = VERR_NOT_SUPPORTED;
715#endif
716 }
717 else
718 {
719 /*
720 * Use the kernel RAM mapping.
721 */
722 pMemLnx->Core.pv = phys_to_virt(page_to_phys(pMemLnx->apPages[0]));
723 Assert(pMemLnx->Core.pv);
724 }
725
726 return rc;
727}
728
729
730/**
731 * Undoes what rtR0MemObjLinuxVMap() did.
732 *
733 * @param pMemLnx The linux memory object.
734 */
735static void rtR0MemObjLinuxVUnmap(PRTR0MEMOBJLNX pMemLnx)
736{
737#if RTLNX_VER_MIN(2,4,22)
738# ifdef IPRT_USE_ALLOC_VM_AREA_FOR_EXEC
739 if (pMemLnx->pArea)
740 {
741# if 0
742 pte_t **papPtes = pMemLnx->papPtesForArea;
743 size_t i;
744 for (i = 0; i < pMemLnx->cPages; i++)
745 *papPtes[i] = 0;
746# endif
747 free_vm_area(pMemLnx->pArea);
748 kfree(pMemLnx->papPtesForArea);
749 pMemLnx->pArea = NULL;
750 pMemLnx->papPtesForArea = NULL;
751 }
752 else
753# endif
754 if (pMemLnx->fMappedToRing0)
755 {
756 Assert(pMemLnx->Core.pv);
757 vunmap(pMemLnx->Core.pv);
758 pMemLnx->fMappedToRing0 = false;
759 }
760#else /* < 2.4.22 */
761 Assert(!pMemLnx->fMappedToRing0);
762#endif
763 pMemLnx->Core.pv = NULL;
764}
765
766
767DECLHIDDEN(int) rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
768{
769 IPRT_LINUX_SAVE_EFL_AC();
770 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
771
772 /*
773 * Release any memory that we've allocated or locked.
774 */
775 switch (pMemLnx->Core.enmType)
776 {
777 case RTR0MEMOBJTYPE_PAGE:
778 case RTR0MEMOBJTYPE_LOW:
779 case RTR0MEMOBJTYPE_CONT:
780 case RTR0MEMOBJTYPE_PHYS:
781 case RTR0MEMOBJTYPE_PHYS_NC:
782 rtR0MemObjLinuxVUnmap(pMemLnx);
783 rtR0MemObjLinuxFreePages(pMemLnx);
784 break;
785
786 case RTR0MEMOBJTYPE_LARGE_PAGE:
787 {
788 uint32_t const cLargePages = pMemLnx->Core.cb >> (pMemLnx->cLargePageOrder + PAGE_SHIFT);
789 uint32_t iLargePage;
790 for (iLargePage = 0; iLargePage < cLargePages; iLargePage++)
791 __free_pages(pMemLnx->apPages[iLargePage << pMemLnx->cLargePageOrder], pMemLnx->cLargePageOrder);
792 pMemLnx->cPages = 0;
793
794#ifdef IPRT_USE_ALLOC_VM_AREA_FOR_EXEC
795 Assert(!pMemLnx->pArea);
796 Assert(!pMemLnx->papPtesForArea);
797#endif
798 break;
799 }
800
801 case RTR0MEMOBJTYPE_LOCK:
802 if (pMemLnx->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
803 {
804 struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
805 size_t iPage;
806 Assert(pTask);
807 if (pTask && pTask->mm)
808 LNX_MM_DOWN_READ(pTask->mm);
809
810 iPage = pMemLnx->cPages;
811 while (iPage-- > 0)
812 {
813 if (!PageReserved(pMemLnx->apPages[iPage]))
814 SetPageDirty(pMemLnx->apPages[iPage]);
815#if RTLNX_VER_MIN(4,6,0)
816 put_page(pMemLnx->apPages[iPage]);
817#else
818 page_cache_release(pMemLnx->apPages[iPage]);
819#endif
820 }
821
822 if (pTask && pTask->mm)
823 LNX_MM_UP_READ(pTask->mm);
824 }
825 /* else: kernel memory - nothing to do here. */
826 break;
827
828 case RTR0MEMOBJTYPE_RES_VIRT:
829 Assert(pMemLnx->Core.pv);
830 if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
831 {
832 struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
833 Assert(pTask);
834 if (pTask && pTask->mm)
835 rtR0MemObjLinuxDoMunmap(pMemLnx->Core.pv, pMemLnx->Core.cb, pTask);
836 }
837 else
838 {
839 vunmap(pMemLnx->Core.pv);
840
841 Assert(pMemLnx->cPages == 1 && pMemLnx->apPages[0] != NULL);
842 __free_page(pMemLnx->apPages[0]);
843 pMemLnx->apPages[0] = NULL;
844 pMemLnx->cPages = 0;
845 }
846 pMemLnx->Core.pv = NULL;
847 break;
848
849 case RTR0MEMOBJTYPE_MAPPING:
850 Assert(pMemLnx->cPages == 0); Assert(pMemLnx->Core.pv);
851 if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
852 {
853 struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
854 Assert(pTask);
855 if (pTask && pTask->mm)
856 rtR0MemObjLinuxDoMunmap(pMemLnx->Core.pv, pMemLnx->Core.cb, pTask);
857 }
858 else
859 vunmap(pMemLnx->Core.pv);
860 pMemLnx->Core.pv = NULL;
861 break;
862
863 default:
864 AssertMsgFailed(("enmType=%d\n", pMemLnx->Core.enmType));
865 return VERR_INTERNAL_ERROR;
866 }
867 IPRT_LINUX_RESTORE_EFL_ONLY_AC();
868 return VINF_SUCCESS;
869}
870
871
872DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable, const char *pszTag)
873{
874 IPRT_LINUX_SAVE_EFL_AC();
875 PRTR0MEMOBJLNX pMemLnx;
876 int rc;
877
878#if RTLNX_VER_MIN(2,4,22)
879 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, PAGE_SIZE, GFP_HIGHUSER,
880 false /* non-contiguous */, fExecutable, VERR_NO_MEMORY, pszTag);
881#else
882 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, PAGE_SIZE, GFP_USER,
883 false /* non-contiguous */, fExecutable, VERR_NO_MEMORY, pszTag);
884#endif
885 if (RT_SUCCESS(rc))
886 {
887 rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
888 if (RT_SUCCESS(rc))
889 {
890 *ppMem = &pMemLnx->Core;
891 IPRT_LINUX_RESTORE_EFL_AC();
892 return rc;
893 }
894
895 rtR0MemObjLinuxFreePages(pMemLnx);
896 rtR0MemObjDelete(&pMemLnx->Core);
897 }
898
899 IPRT_LINUX_RESTORE_EFL_AC();
900 return rc;
901}
902
903
904DECLHIDDEN(int) rtR0MemObjNativeAllocLarge(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, size_t cbLargePage, uint32_t fFlags,
905 const char *pszTag)
906{
907#ifdef GFP_TRANSHUGE
908 /*
909 * Allocate a memory object structure that's large enough to contain
910 * the page pointer array.
911 */
912# ifdef __GFP_MOVABLE
913 unsigned const fGfp = (GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE;
914# else
915 unsigned const fGfp = (GFP_TRANSHUGE | __GFP_ZERO);
916# endif
917 size_t const cPagesPerLarge = cbLargePage >> PAGE_SHIFT;
918 unsigned const cLargePageOrder = rtR0MemObjLinuxOrder(cPagesPerLarge);
919 size_t const cLargePages = cb >> (cLargePageOrder + PAGE_SHIFT);
920 size_t const cPages = cb >> PAGE_SHIFT;
921 PRTR0MEMOBJLNX pMemLnx;
922
923 Assert(RT_BIT_64(cLargePageOrder + PAGE_SHIFT) == cbLargePage);
924 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJLNX, apPages[cPages]),
925 RTR0MEMOBJTYPE_LARGE_PAGE, NULL, cb, pszTag);
926 if (pMemLnx)
927 {
928 size_t iLargePage;
929
930 pMemLnx->Core.fFlags |= RTR0MEMOBJ_FLAGS_ZERO_AT_ALLOC;
931 pMemLnx->cLargePageOrder = cLargePageOrder;
932 pMemLnx->cPages = cPages;
933
934 /*
935 * Allocate the requested number of large pages.
936 */
937 for (iLargePage = 0; iLargePage < cLargePages; iLargePage++)
938 {
939 struct page *paPages = alloc_pages(fGfp, cLargePageOrder);
940 if (paPages)
941 {
942 size_t const iPageBase = iLargePage << cLargePageOrder;
943 size_t iPage = cPagesPerLarge;
944 while (iPage-- > 0)
945 pMemLnx->apPages[iPageBase + iPage] = &paPages[iPage];
946 }
947 else
948 {
949 /*Log(("rtR0MemObjNativeAllocLarge: cb=%#zx cPages=%#zx cLargePages=%#zx cLargePageOrder=%u cPagesPerLarge=%#zx iLargePage=%#zx -> failed!\n",
950 cb, cPages, cLargePages, cLargePageOrder, cPagesPerLarge, iLargePage, paPages));*/
951 while (iLargePage-- > 0)
952 __free_pages(pMemLnx->apPages[iLargePage << (cLargePageOrder - PAGE_SHIFT)], cLargePageOrder);
953 rtR0MemObjDelete(&pMemLnx->Core);
954 return VERR_NO_MEMORY;
955 }
956 }
957 *ppMem = &pMemLnx->Core;
958 return VINF_SUCCESS;
959 }
960 return VERR_NO_MEMORY;
961
962#else
963 /*
964 * We don't call rtR0MemObjFallbackAllocLarge here as it can be a really
965 * bad idea to trigger the swap daemon and whatnot. So, just fail.
966 */
967 RT_NOREF(ppMem, cb, cbLargePage, fFlags, pszTag);
968 return VERR_NOT_SUPPORTED;
969#endif
970}
971
972
973DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable, const char *pszTag)
974{
975 IPRT_LINUX_SAVE_EFL_AC();
976 PRTR0MEMOBJLNX pMemLnx;
977 int rc;
978
979 /* Try to avoid GFP_DMA. GFM_DMA32 was introduced with Linux 2.6.15. */
980#if (defined(RT_ARCH_AMD64) || defined(CONFIG_X86_PAE)) && defined(GFP_DMA32)
981 /* ZONE_DMA32: 0-4GB */
982 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, PAGE_SIZE, GFP_DMA32,
983 false /* non-contiguous */, fExecutable, VERR_NO_LOW_MEMORY, pszTag);
984 if (RT_FAILURE(rc))
985#endif
986#ifdef RT_ARCH_AMD64
987 /* ZONE_DMA: 0-16MB */
988 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, PAGE_SIZE, GFP_DMA,
989 false /* non-contiguous */, fExecutable, VERR_NO_LOW_MEMORY, pszTag);
990#else
991# ifdef CONFIG_X86_PAE
992# endif
993 /* ZONE_NORMAL: 0-896MB */
994 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, PAGE_SIZE, GFP_USER,
995 false /* non-contiguous */, fExecutable, VERR_NO_LOW_MEMORY, pszTag);
996#endif
997 if (RT_SUCCESS(rc))
998 {
999 rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
1000 if (RT_SUCCESS(rc))
1001 {
1002 *ppMem = &pMemLnx->Core;
1003 IPRT_LINUX_RESTORE_EFL_AC();
1004 return rc;
1005 }
1006
1007 rtR0MemObjLinuxFreePages(pMemLnx);
1008 rtR0MemObjDelete(&pMemLnx->Core);
1009 }
1010
1011 IPRT_LINUX_RESTORE_EFL_AC();
1012 return rc;
1013}
1014
1015
1016DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest,
1017 bool fExecutable, const char *pszTag)
1018{
1019 IPRT_LINUX_SAVE_EFL_AC();
1020 PRTR0MEMOBJLNX pMemLnx;
1021 int rc;
1022 uint32_t idxZone;
1023
1024 /*
1025 * The last zone must be able to satisfy the PhysHighest requirement or there
1026 * will be no zone at all.
1027 */
1028 if (g_aZones[RT_ELEMENTS(g_aZones) - 1].PhysHighest > PhysHighest)
1029 {
1030 IPRT_LINUX_RESTORE_EFL_AC();
1031 AssertMsgFailedReturn(("No zone can satisfy PhysHighest=%RHp!\n", PhysHighest),
1032 VERR_NO_CONT_MEMORY);
1033 }
1034
1035 /* Find the first zone matching our PhysHighest requirement. */
1036 idxZone = 0;
1037 for (;;)
1038 {
1039 if (g_aZones[idxZone].PhysHighest <= PhysHighest)
1040 break; /* We found a zone satisfying the requirement. */
1041 idxZone++;
1042 }
1043
1044 /* Now try to allocate pages from all the left zones until one succeeds. */
1045 for (;;)
1046 {
1047 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, PAGE_SIZE, g_aZones[idxZone].fGfp,
1048 true /* contiguous */, fExecutable, VERR_NO_CONT_MEMORY, pszTag);
1049 idxZone++;
1050 if (RT_SUCCESS(rc) || idxZone == RT_ELEMENTS(g_aZones))
1051 break;
1052 }
1053 if (RT_SUCCESS(rc))
1054 {
1055 rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
1056 if (RT_SUCCESS(rc))
1057 {
1058#if defined(RT_STRICT)
1059 size_t iPage = pMemLnx->cPages;
1060 while (iPage-- > 0)
1061 Assert(page_to_phys(pMemLnx->apPages[iPage]) < PhysHighest);
1062#endif
1063 pMemLnx->Core.u.Cont.Phys = page_to_phys(pMemLnx->apPages[0]);
1064 *ppMem = &pMemLnx->Core;
1065 IPRT_LINUX_RESTORE_EFL_AC();
1066 return rc;
1067 }
1068
1069 rtR0MemObjLinuxFreePages(pMemLnx);
1070 rtR0MemObjDelete(&pMemLnx->Core);
1071 }
1072
1073 IPRT_LINUX_RESTORE_EFL_AC();
1074 return rc;
1075}
1076
1077
1078/**
1079 * Worker for rtR0MemObjLinuxAllocPhysSub that tries one allocation strategy.
1080 *
1081 * @returns IPRT status code.
1082 * @param ppMemLnx Where to
1083 * @param enmType The object type.
1084 * @param cb The size of the allocation.
1085 * @param uAlignment The alignment of the physical memory.
1086 * Only valid for fContiguous == true, ignored otherwise.
1087 * @param PhysHighest See rtR0MemObjNativeAllocPhys.
1088 * @param pszTag Allocation tag used for statistics and such.
1089 * @param fGfp The Linux GFP flags to use for the allocation.
1090 */
1091static int rtR0MemObjLinuxAllocPhysSub2(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType,
1092 size_t cb, size_t uAlignment, RTHCPHYS PhysHighest, const char *pszTag, gfp_t fGfp)
1093{
1094 PRTR0MEMOBJLNX pMemLnx;
1095 int rc = rtR0MemObjLinuxAllocPages(&pMemLnx, enmType, cb, uAlignment, fGfp,
1096 enmType == RTR0MEMOBJTYPE_PHYS /* contiguous / non-contiguous */,
1097 false /*fExecutable*/, VERR_NO_PHYS_MEMORY, pszTag);
1098 if (RT_FAILURE(rc))
1099 return rc;
1100
1101 /*
1102 * Check the addresses if necessary. (Can be optimized a bit for PHYS.)
1103 */
1104 if (PhysHighest != NIL_RTHCPHYS)
1105 {
1106 size_t iPage = pMemLnx->cPages;
1107 while (iPage-- > 0)
1108 if (page_to_phys(pMemLnx->apPages[iPage]) > PhysHighest)
1109 {
1110 rtR0MemObjLinuxFreePages(pMemLnx);
1111 rtR0MemObjDelete(&pMemLnx->Core);
1112 return VERR_NO_MEMORY;
1113 }
1114 }
1115
1116 /*
1117 * Complete the object.
1118 */
1119 if (enmType == RTR0MEMOBJTYPE_PHYS)
1120 {
1121 pMemLnx->Core.u.Phys.PhysBase = page_to_phys(pMemLnx->apPages[0]);
1122 pMemLnx->Core.u.Phys.fAllocated = true;
1123 }
1124 *ppMem = &pMemLnx->Core;
1125 return rc;
1126}
1127
1128
1129/**
1130 * Worker for rtR0MemObjNativeAllocPhys and rtR0MemObjNativeAllocPhysNC.
1131 *
1132 * @returns IPRT status code.
1133 * @param ppMem Where to store the memory object pointer on success.
1134 * @param enmType The object type.
1135 * @param cb The size of the allocation.
1136 * @param uAlignment The alignment of the physical memory.
1137 * Only valid for enmType == RTR0MEMOBJTYPE_PHYS, ignored otherwise.
1138 * @param PhysHighest See rtR0MemObjNativeAllocPhys.
1139 * @param pszTag Allocation tag used for statistics and such.
1140 */
1141static int rtR0MemObjLinuxAllocPhysSub(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType,
1142 size_t cb, size_t uAlignment, RTHCPHYS PhysHighest, const char *pszTag)
1143{
1144 int rc;
1145 IPRT_LINUX_SAVE_EFL_AC();
1146
1147 /*
1148 * There are two clear cases and that's the <=16MB and anything-goes ones.
1149 * When the physical address limit is somewhere in-between those two we'll
1150 * just have to try, starting with HIGHUSER and working our way thru the
1151 * different types, hoping we'll get lucky.
1152 *
1153 * We should probably move this physical address restriction logic up to
1154 * the page alloc function as it would be more efficient there. But since
1155 * we don't expect this to be a performance issue just yet it can wait.
1156 */
1157 if (PhysHighest == NIL_RTHCPHYS)
1158 /* ZONE_HIGHMEM: the whole physical memory */
1159 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, pszTag, GFP_HIGHUSER);
1160 else if (PhysHighest <= _1M * 16)
1161 /* ZONE_DMA: 0-16MB */
1162 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, pszTag, GFP_DMA);
1163 else
1164 {
1165 rc = VERR_NO_MEMORY;
1166 if (RT_FAILURE(rc))
1167 /* ZONE_HIGHMEM: the whole physical memory */
1168 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, pszTag, GFP_HIGHUSER);
1169 if (RT_FAILURE(rc))
1170 /* ZONE_NORMAL: 0-896MB */
1171 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, pszTag, GFP_USER);
1172#ifdef GFP_DMA32
1173 if (RT_FAILURE(rc))
1174 /* ZONE_DMA32: 0-4GB */
1175 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, pszTag, GFP_DMA32);
1176#endif
1177 if (RT_FAILURE(rc))
1178 /* ZONE_DMA: 0-16MB */
1179 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, pszTag, GFP_DMA);
1180 }
1181 IPRT_LINUX_RESTORE_EFL_AC();
1182 return rc;
1183}
1184
1185
1186/**
1187 * Translates a kernel virtual address to a linux page structure by walking the
1188 * page tables.
1189 *
1190 * @note We do assume that the page tables will not change as we are walking
1191 * them. This assumption is rather forced by the fact that I could not
1192 * immediately see any way of preventing this from happening. So, we
1193 * take some extra care when accessing them.
1194 *
1195 * Because of this, we don't want to use this function on memory where
1196 * attribute changes to nearby pages is likely to cause large pages to
1197 * be used or split up. So, don't use this for the linear mapping of
1198 * physical memory.
1199 *
1200 * @returns Pointer to the page structur or NULL if it could not be found.
1201 * @param pv The kernel virtual address.
1202 */
1203RTDECL(struct page *) rtR0MemObjLinuxVirtToPage(void *pv)
1204{
1205#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
1206 unsigned long ulAddr = (unsigned long)pv;
1207 unsigned long pfn;
1208 struct page *pPage;
1209 pte_t *pEntry;
1210 union
1211 {
1212 pgd_t Global;
1213# if RTLNX_VER_MIN(4,12,0)
1214 p4d_t Four;
1215# endif
1216# if RTLNX_VER_MIN(2,6,11)
1217 pud_t Upper;
1218# endif
1219 pmd_t Middle;
1220 pte_t Entry;
1221 } u;
1222
1223 /* Should this happen in a situation this code will be called in? And if
1224 * so, can it change under our feet? See also
1225 * "Documentation/vm/active_mm.txt" in the kernel sources. */
1226 if (RT_UNLIKELY(!current->active_mm))
1227 return NULL;
1228 u.Global = *pgd_offset(current->active_mm, ulAddr);
1229 if (RT_UNLIKELY(pgd_none(u.Global)))
1230 return NULL;
1231# if RTLNX_VER_MIN(2,6,11)
1232# if RTLNX_VER_MIN(4,12,0)
1233 u.Four = *p4d_offset(&u.Global, ulAddr);
1234 if (RT_UNLIKELY(p4d_none(u.Four)))
1235 return NULL;
1236# if RTLNX_VER_MIN(5,6,0)
1237 if (p4d_leaf(u.Four))
1238# else
1239 if (p4d_large(u.Four))
1240# endif
1241 {
1242 pPage = p4d_page(u.Four);
1243 AssertReturn(pPage, NULL);
1244 pfn = page_to_pfn(pPage); /* doing the safe way... */
1245 AssertCompile(P4D_SHIFT - PAGE_SHIFT < 31);
1246 pfn += (ulAddr >> PAGE_SHIFT) & ((UINT32_C(1) << (P4D_SHIFT - PAGE_SHIFT)) - 1);
1247 return pfn_to_page(pfn);
1248 }
1249 u.Upper = *pud_offset(&u.Four, ulAddr);
1250# else /* < 4.12 */
1251 u.Upper = *pud_offset(&u.Global, ulAddr);
1252# endif /* < 4.12 */
1253 if (RT_UNLIKELY(pud_none(u.Upper)))
1254 return NULL;
1255# if RTLNX_VER_MIN(2,6,25)
1256# if RTLNX_VER_MIN(5,6,0)
1257 if (pud_leaf(u.Upper))
1258# else
1259 if (pud_large(u.Upper))
1260# endif
1261 {
1262 pPage = pud_page(u.Upper);
1263 AssertReturn(pPage, NULL);
1264 pfn = page_to_pfn(pPage); /* doing the safe way... */
1265 pfn += (ulAddr >> PAGE_SHIFT) & ((UINT32_C(1) << (PUD_SHIFT - PAGE_SHIFT)) - 1);
1266 return pfn_to_page(pfn);
1267 }
1268# endif
1269 u.Middle = *pmd_offset(&u.Upper, ulAddr);
1270# else /* < 2.6.11 */
1271 u.Middle = *pmd_offset(&u.Global, ulAddr);
1272# endif /* < 2.6.11 */
1273 if (RT_UNLIKELY(pmd_none(u.Middle)))
1274 return NULL;
1275# if RTLNX_VER_MIN(2,6,0)
1276# if RTLNX_VER_MIN(5,6,0)
1277 if (pmd_leaf(u.Middle))
1278# else
1279 if (pmd_large(u.Middle))
1280# endif
1281 {
1282 pPage = pmd_page(u.Middle);
1283 AssertReturn(pPage, NULL);
1284 pfn = page_to_pfn(pPage); /* doing the safe way... */
1285 pfn += (ulAddr >> PAGE_SHIFT) & ((UINT32_C(1) << (PMD_SHIFT - PAGE_SHIFT)) - 1);
1286 return pfn_to_page(pfn);
1287 }
1288# endif
1289
1290# if RTLNX_VER_MIN(6,5,0) || RTLNX_RHEL_RANGE(9,4, 9,99)
1291 pEntry = __pte_map(&u.Middle, ulAddr);
1292# elif RTLNX_VER_MIN(2,5,5) || defined(pte_offset_map) /* As usual, RHEL 3 had pte_offset_map earlier. */
1293 pEntry = pte_offset_map(&u.Middle, ulAddr);
1294# else
1295 pEntry = pte_offset(&u.Middle, ulAddr);
1296# endif
1297 if (RT_UNLIKELY(!pEntry))
1298 return NULL;
1299 u.Entry = *pEntry;
1300# if RTLNX_VER_MIN(2,5,5) || defined(pte_offset_map)
1301 pte_unmap(pEntry);
1302# endif
1303
1304 if (RT_UNLIKELY(!pte_present(u.Entry)))
1305 return NULL;
1306 return pte_page(u.Entry);
1307#else /* !defined(RT_ARCH_AMD64) && !defined(RT_ARCH_X86) */
1308
1309 if (is_vmalloc_addr(pv))
1310 return vmalloc_to_page(pv);
1311
1312 return virt_to_page(pv);
1313#endif
1314}
1315RT_EXPORT_SYMBOL(rtR0MemObjLinuxVirtToPage);
1316
1317
1318DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment,
1319 const char *pszTag)
1320{
1321 return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS, cb, uAlignment, PhysHighest, pszTag);
1322}
1323
1324
1325DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, const char *pszTag)
1326{
1327 return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS_NC, cb, PAGE_SIZE, PhysHighest, pszTag);
1328}
1329
1330
1331DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy,
1332 const char *pszTag)
1333{
1334 IPRT_LINUX_SAVE_EFL_AC();
1335
1336 /*
1337 * All we need to do here is to validate that we can use
1338 * ioremap on the specified address (32/64-bit dma_addr_t).
1339 */
1340 PRTR0MEMOBJLNX pMemLnx;
1341 dma_addr_t PhysAddr = Phys;
1342 AssertMsgReturn(PhysAddr == Phys, ("%#llx\n", (unsigned long long)Phys), VERR_ADDRESS_TOO_BIG);
1343
1344 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_PHYS, NULL, cb, pszTag);
1345 if (!pMemLnx)
1346 {
1347 IPRT_LINUX_RESTORE_EFL_AC();
1348 return VERR_NO_MEMORY;
1349 }
1350
1351 pMemLnx->Core.u.Phys.PhysBase = PhysAddr;
1352 pMemLnx->Core.u.Phys.fAllocated = false;
1353 pMemLnx->Core.u.Phys.uCachePolicy = uCachePolicy;
1354 Assert(!pMemLnx->cPages);
1355 *ppMem = &pMemLnx->Core;
1356 IPRT_LINUX_RESTORE_EFL_AC();
1357 return VINF_SUCCESS;
1358}
1359
1360/* openSUSE Leap 42.3 detection :-/ */
1361#if RTLNX_VER_RANGE(4,4,0, 4,6,0) && defined(FAULT_FLAG_REMOTE)
1362# define GET_USER_PAGES_API KERNEL_VERSION(4, 10, 0) /* no typo! */
1363#else
1364# define GET_USER_PAGES_API LINUX_VERSION_CODE
1365#endif
1366
1367DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess,
1368 RTR0PROCESS R0Process, const char *pszTag)
1369{
1370 IPRT_LINUX_SAVE_EFL_AC();
1371 const int cPages = cb >> PAGE_SHIFT;
1372 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1373# if GET_USER_PAGES_API < KERNEL_VERSION(6, 5, 0)
1374 struct vm_area_struct **papVMAs;
1375# endif
1376 PRTR0MEMOBJLNX pMemLnx;
1377 int rc = VERR_NO_MEMORY;
1378 int const fWrite = fAccess & RTMEM_PROT_WRITE ? 1 : 0;
1379
1380 /*
1381 * Check for valid task and size overflows.
1382 */
1383 if (!pTask)
1384 return VERR_NOT_SUPPORTED;
1385 if (((size_t)cPages << PAGE_SHIFT) != cb)
1386 return VERR_OUT_OF_RANGE;
1387
1388 /*
1389 * Allocate the memory object and a temporary buffer for the VMAs.
1390 */
1391 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK,
1392 (void *)R3Ptr, cb, pszTag);
1393 if (!pMemLnx)
1394 {
1395 IPRT_LINUX_RESTORE_EFL_AC();
1396 return VERR_NO_MEMORY;
1397 }
1398
1399# if GET_USER_PAGES_API < KERNEL_VERSION(6, 5, 0)
1400 papVMAs = (struct vm_area_struct **)RTMemAlloc(sizeof(*papVMAs) * cPages);
1401 if (papVMAs)
1402 {
1403# endif
1404 LNX_MM_DOWN_READ(pTask->mm);
1405
1406 /*
1407 * Get user pages.
1408 */
1409/** @todo r=bird: Should we not force read access too? */
1410#if GET_USER_PAGES_API >= KERNEL_VERSION(4, 6, 0)
1411 if (R0Process == RTR0ProcHandleSelf())
1412 rc = get_user_pages(R3Ptr, /* Where from. */
1413 cPages, /* How many pages. */
1414# if GET_USER_PAGES_API >= KERNEL_VERSION(4, 9, 0)
1415 fWrite ? FOLL_WRITE | /* Write to memory. */
1416 FOLL_FORCE /* force write access. */
1417 : 0, /* Write to memory. */
1418# else
1419 fWrite, /* Write to memory. */
1420 fWrite, /* force write access. */
1421# endif
1422 &pMemLnx->apPages[0] /* Page array. */
1423# if GET_USER_PAGES_API < KERNEL_VERSION(6, 5, 0) && !RTLNX_SUSE_MAJ_PREREQ(15, 6)
1424 , papVMAs /* vmas */
1425# endif
1426 );
1427 /*
1428 * Actually this should not happen at the moment as call this function
1429 * only for our own process.
1430 */
1431 else
1432 rc = get_user_pages_remote(
1433# if GET_USER_PAGES_API < KERNEL_VERSION(5, 9, 0)
1434 pTask, /* Task for fault accounting. */
1435# endif
1436 pTask->mm, /* Whose pages. */
1437 R3Ptr, /* Where from. */
1438 cPages, /* How many pages. */
1439# if GET_USER_PAGES_API >= KERNEL_VERSION(4, 9, 0)
1440 fWrite ? FOLL_WRITE | /* Write to memory. */
1441 FOLL_FORCE /* force write access. */
1442 : 0, /* Write to memory. */
1443# else
1444 fWrite, /* Write to memory. */
1445 fWrite, /* force write access. */
1446# endif
1447 &pMemLnx->apPages[0] /* Page array. */
1448# if GET_USER_PAGES_API < KERNEL_VERSION(6, 5, 0)
1449 , papVMAs /* vmas */
1450# endif
1451# if GET_USER_PAGES_API >= KERNEL_VERSION(4, 10, 0)
1452 , NULL /* locked */
1453# endif
1454 );
1455#else /* GET_USER_PAGES_API < KERNEL_VERSION(4, 6, 0) */
1456 rc = get_user_pages(pTask, /* Task for fault accounting. */
1457 pTask->mm, /* Whose pages. */
1458 R3Ptr, /* Where from. */
1459 cPages, /* How many pages. */
1460/* The get_user_pages API change was back-ported to 4.4.168. */
1461# if RTLNX_VER_RANGE(4,4,168, 4,5,0)
1462 fWrite ? FOLL_WRITE | /* Write to memory. */
1463 FOLL_FORCE /* force write access. */
1464 : 0, /* Write to memory. */
1465# else
1466 fWrite, /* Write to memory. */
1467 fWrite, /* force write access. */
1468# endif
1469 &pMemLnx->apPages[0], /* Page array. */
1470 papVMAs /* vmas */
1471 );
1472#endif /* GET_USER_PAGES_API < KERNEL_VERSION(4, 6, 0) */
1473 if (rc == cPages)
1474 {
1475 /*
1476 * Flush dcache (required?), protect against fork and _really_ pin the page
1477 * table entries. get_user_pages() will protect against swapping out the
1478 * pages but it will NOT protect against removing page table entries. This
1479 * can be achieved with
1480 * - using mlock / mmap(..., MAP_LOCKED, ...) from userland. This requires
1481 * an appropriate limit set up with setrlimit(..., RLIMIT_MEMLOCK, ...).
1482 * Usual Linux distributions support only a limited size of locked pages
1483 * (e.g. 32KB).
1484 * - setting the PageReserved bit (as we do in rtR0MemObjLinuxAllocPages()
1485 * or by
1486 * - setting the VM_LOCKED flag. This is the same as doing mlock() without
1487 * a range check.
1488 */
1489 /** @todo The Linux fork() protection will require more work if this API
1490 * is to be used for anything but locking VM pages. */
1491 while (rc-- > 0)
1492 {
1493 flush_dcache_page(pMemLnx->apPages[rc]);
1494# if GET_USER_PAGES_API < KERNEL_VERSION(6, 5, 0) && !RTLNX_SUSE_MAJ_PREREQ(15, 6) && !RTLNX_RHEL_RANGE(9,5, 9,99)
1495# if RTLNX_VER_MIN(6,3,0)
1496 vm_flags_set(papVMAs[rc], VM_DONTCOPY | VM_LOCKED);
1497# else
1498 papVMAs[rc]->vm_flags |= VM_DONTCOPY | VM_LOCKED;
1499# endif
1500# endif
1501 }
1502
1503 LNX_MM_UP_READ(pTask->mm);
1504
1505# if GET_USER_PAGES_API < KERNEL_VERSION(6, 5, 0)
1506 RTMemFree(papVMAs);
1507# endif
1508
1509 pMemLnx->Core.u.Lock.R0Process = R0Process;
1510 pMemLnx->cPages = cPages;
1511 Assert(!pMemLnx->fMappedToRing0);
1512 *ppMem = &pMemLnx->Core;
1513
1514 IPRT_LINUX_RESTORE_EFL_AC();
1515 return VINF_SUCCESS;
1516 }
1517
1518 /*
1519 * Failed - we need to unlock any pages that we succeeded to lock.
1520 */
1521 while (rc-- > 0)
1522 {
1523 if (!PageReserved(pMemLnx->apPages[rc]))
1524 SetPageDirty(pMemLnx->apPages[rc]);
1525#if RTLNX_VER_MIN(4,6,0)
1526 put_page(pMemLnx->apPages[rc]);
1527#else
1528 page_cache_release(pMemLnx->apPages[rc]);
1529#endif
1530 }
1531
1532 LNX_MM_UP_READ(pTask->mm);
1533
1534 rc = VERR_LOCK_FAILED;
1535
1536# if GET_USER_PAGES_API < KERNEL_VERSION(6, 5, 0)
1537 RTMemFree(papVMAs);
1538 }
1539# endif
1540
1541 rtR0MemObjDelete(&pMemLnx->Core);
1542 IPRT_LINUX_RESTORE_EFL_AC();
1543 return rc;
1544}
1545
1546
1547DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess, const char *pszTag)
1548{
1549 IPRT_LINUX_SAVE_EFL_AC();
1550 void *pvLast = (uint8_t *)pv + cb - 1;
1551 size_t const cPages = cb >> PAGE_SHIFT;
1552 PRTR0MEMOBJLNX pMemLnx;
1553 bool fLinearMapping;
1554 int rc;
1555 uint8_t *pbPage;
1556 size_t iPage;
1557 NOREF(fAccess);
1558
1559 if ( !RTR0MemKernelIsValidAddr(pv)
1560 || !RTR0MemKernelIsValidAddr(pv + cb))
1561 return VERR_INVALID_PARAMETER;
1562
1563 /*
1564 * The lower part of the kernel memory has a linear mapping between
1565 * physical and virtual addresses. So we take a short cut here. This is
1566 * assumed to be the cleanest way to handle those addresses (and the code
1567 * is well tested, though the test for determining it is not very nice).
1568 * If we ever decide it isn't we can still remove it.
1569 */
1570#if 0
1571 fLinearMapping = (unsigned long)pvLast < VMALLOC_START;
1572#else
1573 fLinearMapping = (unsigned long)pv >= (unsigned long)__va(0)
1574 && (unsigned long)pvLast < (unsigned long)high_memory;
1575#endif
1576
1577 /*
1578 * Allocate the memory object.
1579 */
1580 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK,
1581 pv, cb, pszTag);
1582 if (!pMemLnx)
1583 {
1584 IPRT_LINUX_RESTORE_EFL_AC();
1585 return VERR_NO_MEMORY;
1586 }
1587
1588 /*
1589 * Gather the pages.
1590 * We ASSUME all kernel pages are non-swappable and non-movable.
1591 */
1592 rc = VINF_SUCCESS;
1593 pbPage = (uint8_t *)pvLast;
1594 iPage = cPages;
1595 if (!fLinearMapping)
1596 {
1597 while (iPage-- > 0)
1598 {
1599 struct page *pPage = rtR0MemObjLinuxVirtToPage(pbPage);
1600 if (RT_UNLIKELY(!pPage))
1601 {
1602 rc = VERR_LOCK_FAILED;
1603 break;
1604 }
1605 pMemLnx->apPages[iPage] = pPage;
1606 pbPage -= PAGE_SIZE;
1607 }
1608 }
1609 else
1610 {
1611 while (iPage-- > 0)
1612 {
1613 pMemLnx->apPages[iPage] = virt_to_page(pbPage);
1614 pbPage -= PAGE_SIZE;
1615 }
1616 }
1617 if (RT_SUCCESS(rc))
1618 {
1619 /*
1620 * Complete the memory object and return.
1621 */
1622 pMemLnx->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
1623 pMemLnx->cPages = cPages;
1624 Assert(!pMemLnx->fMappedToRing0);
1625 *ppMem = &pMemLnx->Core;
1626
1627 IPRT_LINUX_RESTORE_EFL_AC();
1628 return VINF_SUCCESS;
1629 }
1630
1631 rtR0MemObjDelete(&pMemLnx->Core);
1632 IPRT_LINUX_RESTORE_EFL_AC();
1633 return rc;
1634}
1635
1636
1637DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment,
1638 const char *pszTag)
1639{
1640#if RTLNX_VER_MIN(2,4,22)
1641 IPRT_LINUX_SAVE_EFL_AC();
1642 const size_t cPages = cb >> PAGE_SHIFT;
1643 struct page *pDummyPage;
1644 struct page **papPages;
1645
1646 /* check for unsupported stuff. */
1647 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
1648 if (uAlignment > PAGE_SIZE)
1649 return VERR_NOT_SUPPORTED;
1650
1651 /*
1652 * Allocate a dummy page and create a page pointer array for vmap such that
1653 * the dummy page is mapped all over the reserved area.
1654 */
1655 pDummyPage = alloc_page(GFP_HIGHUSER | __GFP_NOWARN);
1656 if (pDummyPage)
1657 {
1658 papPages = RTMemAlloc(sizeof(*papPages) * cPages);
1659 if (papPages)
1660 {
1661 void *pv;
1662 size_t iPage = cPages;
1663 while (iPage-- > 0)
1664 papPages[iPage] = pDummyPage;
1665# ifdef VM_MAP
1666 pv = vmap(papPages, cPages, VM_MAP, PAGE_KERNEL_RO);
1667# else
1668 pv = vmap(papPages, cPages, VM_ALLOC, PAGE_KERNEL_RO);
1669# endif
1670 RTMemFree(papPages);
1671 if (pv)
1672 {
1673 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb, pszTag);
1674 if (pMemLnx)
1675 {
1676 pMemLnx->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
1677 pMemLnx->cPages = 1;
1678 pMemLnx->apPages[0] = pDummyPage;
1679 *ppMem = &pMemLnx->Core;
1680 IPRT_LINUX_RESTORE_EFL_AC();
1681 return VINF_SUCCESS;
1682 }
1683 vunmap(pv);
1684 }
1685 }
1686 __free_page(pDummyPage);
1687 }
1688 IPRT_LINUX_RESTORE_EFL_AC();
1689 return VERR_NO_MEMORY;
1690
1691#else /* < 2.4.22 */
1692 /*
1693 * Could probably use ioremap here, but the caller is in a better position than us
1694 * to select some safe physical memory.
1695 */
1696 return VERR_NOT_SUPPORTED;
1697#endif
1698}
1699
1700
1701DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment,
1702 RTR0PROCESS R0Process, const char *pszTag)
1703{
1704 IPRT_LINUX_SAVE_EFL_AC();
1705 PRTR0MEMOBJLNX pMemLnx;
1706 void *pv;
1707 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1708 if (!pTask)
1709 return VERR_NOT_SUPPORTED;
1710
1711 /*
1712 * Check that the specified alignment is supported.
1713 */
1714 if (uAlignment > PAGE_SIZE)
1715 return VERR_NOT_SUPPORTED;
1716
1717 /*
1718 * Let rtR0MemObjLinuxDoMmap do the difficult bits.
1719 */
1720 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cb, uAlignment, pTask, RTMEM_PROT_NONE);
1721 if (pv == (void *)-1)
1722 {
1723 IPRT_LINUX_RESTORE_EFL_AC();
1724 return VERR_NO_MEMORY;
1725 }
1726
1727 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb, pszTag);
1728 if (!pMemLnx)
1729 {
1730 rtR0MemObjLinuxDoMunmap(pv, cb, pTask);
1731 IPRT_LINUX_RESTORE_EFL_AC();
1732 return VERR_NO_MEMORY;
1733 }
1734
1735 pMemLnx->Core.u.ResVirt.R0Process = R0Process;
1736 *ppMem = &pMemLnx->Core;
1737 IPRT_LINUX_RESTORE_EFL_AC();
1738 return VINF_SUCCESS;
1739}
1740
1741
1742DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
1743 unsigned fProt, size_t offSub, size_t cbSub, const char *pszTag)
1744{
1745 int rc = VERR_NO_MEMORY;
1746 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1747 PRTR0MEMOBJLNX pMemLnx;
1748 IPRT_LINUX_SAVE_EFL_AC();
1749
1750 /* Fail if requested to do something we can't. */
1751 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
1752 if (uAlignment > PAGE_SIZE)
1753 return VERR_NOT_SUPPORTED;
1754
1755 /*
1756 * Create the IPRT memory object.
1757 */
1758 if (!cbSub)
1759 cbSub = pMemLnxToMap->Core.cb - offSub;
1760 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, cbSub, pszTag);
1761 if (pMemLnx)
1762 {
1763 if (pMemLnxToMap->cPages)
1764 {
1765#if RTLNX_VER_MIN(2,4,22)
1766 /*
1767 * Use vmap - 2.4.22 and later.
1768 */
1769 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, true /* kernel */);
1770 /** @todo We don't really care too much for EXEC here... 5.8 always adds NX. */
1771 Assert(((offSub + cbSub) >> PAGE_SHIFT) <= pMemLnxToMap->cPages);
1772# ifdef VM_MAP
1773 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[offSub >> PAGE_SHIFT], cbSub >> PAGE_SHIFT, VM_MAP, fPg);
1774# else
1775 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[offSub >> PAGE_SHIFT], cbSub >> PAGE_SHIFT, VM_ALLOC, fPg);
1776# endif
1777 if (pMemLnx->Core.pv)
1778 {
1779 pMemLnx->fMappedToRing0 = true;
1780 rc = VINF_SUCCESS;
1781 }
1782 else
1783 rc = VERR_MAP_FAILED;
1784
1785#else /* < 2.4.22 */
1786 /*
1787 * Only option here is to share mappings if possible and forget about fProt.
1788 */
1789 if (rtR0MemObjIsRing3(pMemToMap))
1790 rc = VERR_NOT_SUPPORTED;
1791 else
1792 {
1793 rc = VINF_SUCCESS;
1794 if (!pMemLnxToMap->Core.pv)
1795 rc = rtR0MemObjLinuxVMap(pMemLnxToMap, !!(fProt & RTMEM_PROT_EXEC));
1796 if (RT_SUCCESS(rc))
1797 {
1798 Assert(pMemLnxToMap->Core.pv);
1799 pMemLnx->Core.pv = (uint8_t *)pMemLnxToMap->Core.pv + offSub;
1800 }
1801 }
1802#endif
1803 }
1804 else
1805 {
1806 /*
1807 * MMIO / physical memory.
1808 */
1809 Assert(pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS && !pMemLnxToMap->Core.u.Phys.fAllocated);
1810#if RTLNX_VER_MIN(2,6,25)
1811 /*
1812 * ioremap() defaults to no caching since the 2.6 kernels.
1813 * ioremap_nocache() has been removed finally in 5.6-rc1.
1814 */
1815 pMemLnx->Core.pv = pMemLnxToMap->Core.u.Phys.uCachePolicy == RTMEM_CACHE_POLICY_MMIO
1816 ? ioremap(pMemLnxToMap->Core.u.Phys.PhysBase + offSub, cbSub)
1817 : ioremap_cache(pMemLnxToMap->Core.u.Phys.PhysBase + offSub, cbSub);
1818#else /* KERNEL_VERSION < 2.6.25 */
1819 pMemLnx->Core.pv = pMemLnxToMap->Core.u.Phys.uCachePolicy == RTMEM_CACHE_POLICY_MMIO
1820 ? ioremap_nocache(pMemLnxToMap->Core.u.Phys.PhysBase + offSub, cbSub)
1821 : ioremap(pMemLnxToMap->Core.u.Phys.PhysBase + offSub, cbSub);
1822#endif /* KERNEL_VERSION < 2.6.25 */
1823 if (pMemLnx->Core.pv)
1824 {
1825 /** @todo fix protection. */
1826 rc = VINF_SUCCESS;
1827 }
1828 }
1829 if (RT_SUCCESS(rc))
1830 {
1831 pMemLnx->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
1832 *ppMem = &pMemLnx->Core;
1833 IPRT_LINUX_RESTORE_EFL_AC();
1834 return VINF_SUCCESS;
1835 }
1836 rtR0MemObjDelete(&pMemLnx->Core);
1837 }
1838
1839 IPRT_LINUX_RESTORE_EFL_AC();
1840 return rc;
1841}
1842
1843
1844#ifdef VBOX_USE_PAE_HACK
1845/**
1846 * Replace the PFN of a PTE with the address of the actual page.
1847 *
1848 * The caller maps a reserved dummy page at the address with the desired access
1849 * and flags.
1850 *
1851 * This hack is required for older Linux kernels which don't provide
1852 * remap_pfn_range().
1853 *
1854 * @returns 0 on success, -ENOMEM on failure.
1855 * @param mm The memory context.
1856 * @param ulAddr The mapping address.
1857 * @param Phys The physical address of the page to map.
1858 */
1859static int rtR0MemObjLinuxFixPte(struct mm_struct *mm, unsigned long ulAddr, RTHCPHYS Phys)
1860{
1861 int rc = -ENOMEM;
1862 pgd_t *pgd;
1863
1864 spin_lock(&mm->page_table_lock);
1865
1866 pgd = pgd_offset(mm, ulAddr);
1867 if (!pgd_none(*pgd) && !pgd_bad(*pgd))
1868 {
1869 pmd_t *pmd = pmd_offset(pgd, ulAddr);
1870 if (!pmd_none(*pmd))
1871 {
1872 pte_t *ptep = pte_offset_map(pmd, ulAddr);
1873 if (ptep)
1874 {
1875 pte_t pte = *ptep;
1876 pte.pte_high &= 0xfff00000;
1877 pte.pte_high |= ((Phys >> 32) & 0x000fffff);
1878 pte.pte_low &= 0x00000fff;
1879 pte.pte_low |= (Phys & 0xfffff000);
1880 set_pte(ptep, pte);
1881 pte_unmap(ptep);
1882 rc = 0;
1883 }
1884 }
1885 }
1886
1887 spin_unlock(&mm->page_table_lock);
1888 return rc;
1889}
1890#endif /* VBOX_USE_PAE_HACK */
1891
1892
1893DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment,
1894 unsigned fProt, RTR0PROCESS R0Process, size_t offSub, size_t cbSub, const char *pszTag)
1895{
1896 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1897 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1898 int rc = VERR_NO_MEMORY;
1899 PRTR0MEMOBJLNX pMemLnx;
1900#ifdef VBOX_USE_PAE_HACK
1901 struct page *pDummyPage;
1902 RTHCPHYS DummyPhys;
1903#endif
1904 IPRT_LINUX_SAVE_EFL_AC();
1905
1906 /*
1907 * Check for restrictions.
1908 */
1909 if (!pTask)
1910 return VERR_NOT_SUPPORTED;
1911 if (uAlignment > PAGE_SIZE)
1912 return VERR_NOT_SUPPORTED;
1913
1914#ifdef VBOX_USE_PAE_HACK
1915 /*
1916 * Allocate a dummy page for use when mapping the memory.
1917 */
1918 pDummyPage = alloc_page(GFP_USER | __GFP_NOWARN);
1919 if (!pDummyPage)
1920 {
1921 IPRT_LINUX_RESTORE_EFL_AC();
1922 return VERR_NO_MEMORY;
1923 }
1924 SetPageReserved(pDummyPage);
1925 DummyPhys = page_to_phys(pDummyPage);
1926#endif
1927
1928 /*
1929 * Create the IPRT memory object.
1930 */
1931 Assert(!offSub || cbSub);
1932 if (cbSub == 0)
1933 cbSub = pMemLnxToMap->Core.cb;
1934 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, cbSub, pszTag);
1935 if (pMemLnx)
1936 {
1937 /*
1938 * Allocate user space mapping.
1939 */
1940 void *pv;
1941 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cbSub, uAlignment, pTask, fProt);
1942 if (pv != (void *)-1)
1943 {
1944 /*
1945 * Map page by page into the mmap area.
1946 * This is generic, paranoid and not very efficient.
1947 */
1948 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, false /* user */);
1949 unsigned long ulAddrCur = (unsigned long)pv;
1950 const size_t cPages = (offSub + cbSub) >> PAGE_SHIFT;
1951 size_t iPage;
1952
1953 LNX_MM_DOWN_WRITE(pTask->mm);
1954
1955 rc = VINF_SUCCESS;
1956 if (pMemLnxToMap->cPages)
1957 {
1958 for (iPage = offSub >> PAGE_SHIFT; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
1959 {
1960#if RTLNX_VER_MAX(2,6,11)
1961 RTHCPHYS Phys = page_to_phys(pMemLnxToMap->apPages[iPage]);
1962#endif
1963#if RTLNX_VER_MIN(2,6,0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1964 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1965 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1966#endif
1967#if RTLNX_VER_MAX(2,6,0) && defined(RT_ARCH_X86)
1968 /* remap_page_range() limitation on x86 */
1969 AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
1970#endif
1971
1972#if defined(VBOX_USE_INSERT_PAGE) && RTLNX_VER_MIN(2,6,22)
1973 rc = vm_insert_page(vma, ulAddrCur, pMemLnxToMap->apPages[iPage]);
1974 /* Thes flags help making 100% sure some bad stuff wont happen (swap, core, ++).
1975 * See remap_pfn_range() in mm/memory.c */
1976
1977#if RTLNX_VER_MIN(6,3,0) || RTLNX_RHEL_RANGE(9,5, 9,99)
1978 vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
1979#elif RTLNX_VER_MIN(3,7,0)
1980 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1981#else
1982 vma->vm_flags |= VM_RESERVED;
1983#endif
1984#elif RTLNX_VER_MIN(2,6,11)
1985 rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1986#elif defined(VBOX_USE_PAE_HACK)
1987 rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
1988 if (!rc)
1989 rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
1990#elif RTLNX_VER_MIN(2,6,0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1991 rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1992#else /* 2.4 */
1993 rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
1994#endif
1995 if (rc)
1996 {
1997 rc = VERR_NO_MEMORY;
1998 break;
1999 }
2000 }
2001 }
2002 else
2003 {
2004 RTHCPHYS Phys;
2005 if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS)
2006 Phys = pMemLnxToMap->Core.u.Phys.PhysBase;
2007 else if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_CONT)
2008 Phys = pMemLnxToMap->Core.u.Cont.Phys;
2009 else
2010 {
2011 AssertMsgFailed(("%d\n", pMemLnxToMap->Core.enmType));
2012 Phys = NIL_RTHCPHYS;
2013 }
2014 if (Phys != NIL_RTHCPHYS)
2015 {
2016 for (iPage = offSub >> PAGE_SHIFT; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE, Phys += PAGE_SIZE)
2017 {
2018#if RTLNX_VER_MIN(2,6,0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
2019 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
2020 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
2021#endif
2022#if RTLNX_VER_MAX(2,6,0) && defined(RT_ARCH_X86)
2023 /* remap_page_range() limitation on x86 */
2024 AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
2025#endif
2026
2027#if RTLNX_VER_MIN(2,6,11)
2028 rc = remap_pfn_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
2029#elif defined(VBOX_USE_PAE_HACK)
2030 rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
2031 if (!rc)
2032 rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
2033#elif RTLNX_VER_MIN(2,6,0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
2034 rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
2035#else /* 2.4 */
2036 rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
2037#endif
2038 if (rc)
2039 {
2040 rc = VERR_NO_MEMORY;
2041 break;
2042 }
2043 }
2044 }
2045 }
2046
2047#ifdef CONFIG_NUMA_BALANCING
2048# if RTLNX_VER_MAX(3,13,0) && RTLNX_RHEL_MAX(7,0)
2049# define VBOX_NUMA_HACK_OLD
2050# endif
2051 if (RT_SUCCESS(rc))
2052 {
2053 /** @todo Ugly hack! But right now we have no other means to
2054 * disable automatic NUMA page balancing. */
2055# ifdef RT_OS_X86
2056# ifdef VBOX_NUMA_HACK_OLD
2057 pTask->mm->numa_next_reset = jiffies + 0x7fffffffUL;
2058# endif
2059 pTask->mm->numa_next_scan = jiffies + 0x7fffffffUL;
2060# else
2061# ifdef VBOX_NUMA_HACK_OLD
2062 pTask->mm->numa_next_reset = jiffies + 0x7fffffffffffffffUL;
2063# endif
2064 pTask->mm->numa_next_scan = jiffies + 0x7fffffffffffffffUL;
2065# endif
2066 }
2067#endif /* CONFIG_NUMA_BALANCING */
2068
2069 LNX_MM_UP_WRITE(pTask->mm);
2070
2071 if (RT_SUCCESS(rc))
2072 {
2073#ifdef VBOX_USE_PAE_HACK
2074 __free_page(pDummyPage);
2075#endif
2076 pMemLnx->Core.pv = pv;
2077 pMemLnx->Core.u.Mapping.R0Process = R0Process;
2078 *ppMem = &pMemLnx->Core;
2079 IPRT_LINUX_RESTORE_EFL_AC();
2080 return VINF_SUCCESS;
2081 }
2082
2083 /*
2084 * Bail out.
2085 */
2086 rtR0MemObjLinuxDoMunmap(pv, cbSub, pTask);
2087 }
2088 rtR0MemObjDelete(&pMemLnx->Core);
2089 }
2090#ifdef VBOX_USE_PAE_HACK
2091 __free_page(pDummyPage);
2092#endif
2093
2094 IPRT_LINUX_RESTORE_EFL_AC();
2095 return rc;
2096}
2097
2098
2099DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
2100{
2101# ifdef IPRT_USE_ALLOC_VM_AREA_FOR_EXEC
2102 /*
2103 * Currently only supported when we've got addresses PTEs from the kernel.
2104 */
2105 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
2106 if (pMemLnx->pArea && pMemLnx->papPtesForArea)
2107 {
2108 pgprot_t const fPg = rtR0MemObjLinuxConvertProt(fProt, true /*fKernel*/);
2109 size_t const cPages = (offSub + cbSub) >> PAGE_SHIFT;
2110 pte_t **papPtes = pMemLnx->papPtesForArea;
2111 size_t i;
2112
2113 for (i = offSub >> PAGE_SHIFT; i < cPages; i++)
2114 {
2115 set_pte(papPtes[i], mk_pte(pMemLnx->apPages[i], fPg));
2116 }
2117 preempt_disable();
2118 __flush_tlb_all();
2119 preempt_enable();
2120 return VINF_SUCCESS;
2121 }
2122# elif defined(IPRT_USE_APPLY_TO_PAGE_RANGE_FOR_EXEC)
2123 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
2124 if ( pMemLnx->fExecutable
2125 && pMemLnx->fMappedToRing0)
2126 {
2127 LNXAPPLYPGRANGE Args;
2128 Args.pMemLnx = pMemLnx;
2129 Args.fPg = rtR0MemObjLinuxConvertProt(fProt, true /*fKernel*/);
2130 int rcLnx = apply_to_page_range(current->active_mm, (unsigned long)pMemLnx->Core.pv + offSub, cbSub,
2131 rtR0MemObjLinuxApplyPageRange, (void *)&Args);
2132 if (rcLnx)
2133 return VERR_NOT_SUPPORTED;
2134
2135 return VINF_SUCCESS;
2136 }
2137# endif
2138
2139 NOREF(pMem);
2140 NOREF(offSub);
2141 NOREF(cbSub);
2142 NOREF(fProt);
2143 return VERR_NOT_SUPPORTED;
2144}
2145
2146
2147DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
2148{
2149 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
2150
2151 if (pMemLnx->cPages)
2152 return page_to_phys(pMemLnx->apPages[iPage]);
2153
2154 switch (pMemLnx->Core.enmType)
2155 {
2156 case RTR0MEMOBJTYPE_CONT:
2157 return pMemLnx->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
2158
2159 case RTR0MEMOBJTYPE_PHYS:
2160 return pMemLnx->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
2161
2162 /* the parent knows */
2163 case RTR0MEMOBJTYPE_MAPPING:
2164 return rtR0MemObjNativeGetPagePhysAddr(pMemLnx->Core.uRel.Child.pParent, iPage);
2165
2166 /* cPages > 0 */
2167 case RTR0MEMOBJTYPE_LOW:
2168 case RTR0MEMOBJTYPE_LOCK:
2169 case RTR0MEMOBJTYPE_PHYS_NC:
2170 case RTR0MEMOBJTYPE_PAGE:
2171 case RTR0MEMOBJTYPE_LARGE_PAGE:
2172 default:
2173 AssertMsgFailed(("%d\n", pMemLnx->Core.enmType));
2174 RT_FALL_THROUGH();
2175
2176 case RTR0MEMOBJTYPE_RES_VIRT:
2177 return NIL_RTHCPHYS;
2178 }
2179}
2180
2181
2182DECLHIDDEN(int) rtR0MemObjNativeZeroInitWithoutMapping(PRTR0MEMOBJINTERNAL pMem)
2183{
2184 PRTR0MEMOBJLNX const pMemLnx = (PRTR0MEMOBJLNX)pMem;
2185 size_t const cPages = pMemLnx->Core.cb >> PAGE_SHIFT;
2186 size_t iPage;
2187 /** @todo optimize this. */
2188 for (iPage = 0; iPage < cPages; iPage++)
2189 {
2190 void *pvPage;
2191
2192 /* Get the physical address of the page. */
2193 RTHCPHYS const HCPhys = rtR0MemObjNativeGetPagePhysAddr(&pMemLnx->Core, iPage);
2194 AssertReturn(HCPhys != NIL_RTHCPHYS, VERR_INTERNAL_ERROR_3);
2195 Assert(!(HCPhys & PAGE_OFFSET_MASK));
2196
2197 /* Would've like to use valid_phys_addr_range for this test, but it isn't exported. */
2198 AssertReturn((HCPhys | PAGE_OFFSET_MASK) < __pa(high_memory), VERR_INTERNAL_ERROR_3);
2199
2200 /* Map it. */
2201 pvPage = phys_to_virt(HCPhys);
2202 AssertPtrReturn(pvPage, VERR_INTERNAL_ERROR_3);
2203
2204 /* Zero it. */
2205 RT_BZERO(pvPage, PAGE_SIZE);
2206 }
2207 return VINF_SUCCESS;
2208}
2209
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