VirtualBox

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

Last change on this file since 91482 was 91482, checked in by vboxsync, 3 years ago

IPRT/memobj: Passing pszTag around...

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette