VirtualBox

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

Last change on this file since 81587 was 81587, checked in by vboxsync, 5 years ago

IPRT/memobj-r0drv-linux.c: Don't mark allocations as executable unless requested (linux < 2.4.22 only). Made the MY_SET_PAGES_EXEC macros be undefined on 5.4.0 and later since they should not be engaged there. (untested) ticketref:18945

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