VirtualBox

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

Last change on this file since 106734 was 106311, checked in by vboxsync, 7 months ago

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

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