VirtualBox

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

Last change on this file was 109076, checked in by vboxsync, 9 days ago

Runtime/r0drv/linux/memobj-r0drv-linux.c: Try using GFP_KERNEL to allow reclaiming memory from the page cache

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