VirtualBox

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

Last change on this file since 77367 was 77367, checked in by vboxsync, 6 years ago

Runtime/r0drv/linux: handle get_user_pages back-port to Linux 4.4.168.
ticketref:18315:Kernel modules do not build with linux kernel 4.4.169.

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

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