VirtualBox

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

Last change on this file since 109071 was 109071, checked in by vboxsync, 4 weeks ago

Runtime/r0drv/linux/memobj-r0drv-linux.c: GFP_REPEAT was renamed to GFP_RETRY_MAYFAIL in 2017 with commit dcda9b04713c3f6ff0875652924844fae28286ea, so we weren't instructing the allocator to try hard to allocate memory with such kernels. Hopefully fixes VERR_NO_LOW_MEMORY errors when starting a VM

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