VirtualBox

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

Last change on this file since 77464 was 77464, 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.
The previous attempt was bad, try again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Revision
File size: 59.3 KB
Line 
1/* $Id: memobj-r0drv-linux.c 77464 2019-02-26 06:48:27Z 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#else
1035# define GET_USER_PAGES_API LINUX_VERSION_CODE
1036#endif
1037
1038DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
1039{
1040 IPRT_LINUX_SAVE_EFL_AC();
1041 const int cPages = cb >> PAGE_SHIFT;
1042 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1043 struct vm_area_struct **papVMAs;
1044 PRTR0MEMOBJLNX pMemLnx;
1045 int rc = VERR_NO_MEMORY;
1046 int const fWrite = fAccess & RTMEM_PROT_WRITE ? 1 : 0;
1047
1048 /*
1049 * Check for valid task and size overflows.
1050 */
1051 if (!pTask)
1052 return VERR_NOT_SUPPORTED;
1053 if (((size_t)cPages << PAGE_SHIFT) != cb)
1054 return VERR_OUT_OF_RANGE;
1055
1056 /*
1057 * Allocate the memory object and a temporary buffer for the VMAs.
1058 */
1059 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
1060 if (!pMemLnx)
1061 {
1062 IPRT_LINUX_RESTORE_EFL_AC();
1063 return VERR_NO_MEMORY;
1064 }
1065
1066 papVMAs = (struct vm_area_struct **)RTMemAlloc(sizeof(*papVMAs) * cPages);
1067 if (papVMAs)
1068 {
1069 down_read(&pTask->mm->mmap_sem);
1070
1071 /*
1072 * Get user pages.
1073 */
1074/** @todo r=bird: Should we not force read access too? */
1075#if GET_USER_PAGES_API >= KERNEL_VERSION(4, 6, 0)
1076 if (R0Process == RTR0ProcHandleSelf())
1077 rc = get_user_pages(R3Ptr, /* Where from. */
1078 cPages, /* How many pages. */
1079# if GET_USER_PAGES_API >= KERNEL_VERSION(4, 9, 0)
1080 fWrite ? FOLL_WRITE | /* Write to memory. */
1081 FOLL_FORCE /* force write access. */
1082 : 0, /* Write to memory. */
1083# else
1084 fWrite, /* Write to memory. */
1085 fWrite, /* force write access. */
1086# endif
1087 &pMemLnx->apPages[0], /* Page array. */
1088 papVMAs); /* vmas */
1089 /*
1090 * Actually this should not happen at the moment as call this function
1091 * only for our own process.
1092 */
1093 else
1094 rc = get_user_pages_remote(
1095 pTask, /* Task for fault accounting. */
1096 pTask->mm, /* Whose pages. */
1097 R3Ptr, /* Where from. */
1098 cPages, /* How many pages. */
1099# if GET_USER_PAGES_API >= KERNEL_VERSION(4, 9, 0)
1100 fWrite ? FOLL_WRITE | /* Write to memory. */
1101 FOLL_FORCE /* force write access. */
1102 : 0, /* Write to memory. */
1103# else
1104 fWrite, /* Write to memory. */
1105 fWrite, /* force write access. */
1106# endif
1107 &pMemLnx->apPages[0], /* Page array. */
1108 papVMAs /* vmas */
1109# if GET_USER_PAGES_API >= KERNEL_VERSION(4, 10, 0)
1110 , NULL /* locked */
1111# endif
1112 );
1113#else /* GET_USER_PAGES_API < KERNEL_VERSION(4, 6, 0) */
1114 rc = get_user_pages(pTask, /* Task for fault accounting. */
1115 pTask->mm, /* Whose pages. */
1116 R3Ptr, /* Where from. */
1117 cPages, /* How many pages. */
1118/* The get_user_pages API change was back-ported to 4.4.168. */
1119# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 168) \
1120 && LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
1121 fWrite ? FOLL_WRITE | /* Write to memory. */
1122 FOLL_FORCE /* force write access. */
1123 : 0, /* Write to memory. */
1124# else
1125 fWrite, /* Write to memory. */
1126 fWrite, /* force write access. */
1127# endif
1128 &pMemLnx->apPages[0], /* Page array. */
1129 papVMAs); /* vmas */
1130#endif /* GET_USER_PAGES_API < KERNEL_VERSION(4, 6, 0) */
1131 if (rc == cPages)
1132 {
1133 /*
1134 * Flush dcache (required?), protect against fork and _really_ pin the page
1135 * table entries. get_user_pages() will protect against swapping out the
1136 * pages but it will NOT protect against removing page table entries. This
1137 * can be achieved with
1138 * - using mlock / mmap(..., MAP_LOCKED, ...) from userland. This requires
1139 * an appropriate limit set up with setrlimit(..., RLIMIT_MEMLOCK, ...).
1140 * Usual Linux distributions support only a limited size of locked pages
1141 * (e.g. 32KB).
1142 * - setting the PageReserved bit (as we do in rtR0MemObjLinuxAllocPages()
1143 * or by
1144 * - setting the VM_LOCKED flag. This is the same as doing mlock() without
1145 * a range check.
1146 */
1147 /** @todo The Linux fork() protection will require more work if this API
1148 * is to be used for anything but locking VM pages. */
1149 while (rc-- > 0)
1150 {
1151 flush_dcache_page(pMemLnx->apPages[rc]);
1152 papVMAs[rc]->vm_flags |= VM_DONTCOPY | VM_LOCKED;
1153 }
1154
1155 up_read(&pTask->mm->mmap_sem);
1156
1157 RTMemFree(papVMAs);
1158
1159 pMemLnx->Core.u.Lock.R0Process = R0Process;
1160 pMemLnx->cPages = cPages;
1161 Assert(!pMemLnx->fMappedToRing0);
1162 *ppMem = &pMemLnx->Core;
1163
1164 IPRT_LINUX_RESTORE_EFL_AC();
1165 return VINF_SUCCESS;
1166 }
1167
1168 /*
1169 * Failed - we need to unlock any pages that we succeeded to lock.
1170 */
1171 while (rc-- > 0)
1172 {
1173 if (!PageReserved(pMemLnx->apPages[rc]))
1174 SetPageDirty(pMemLnx->apPages[rc]);
1175#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 6, 0)
1176 put_page(pMemLnx->apPages[rc]);
1177#else
1178 page_cache_release(pMemLnx->apPages[rc]);
1179#endif
1180 }
1181
1182 up_read(&pTask->mm->mmap_sem);
1183
1184 RTMemFree(papVMAs);
1185 rc = VERR_LOCK_FAILED;
1186 }
1187
1188 rtR0MemObjDelete(&pMemLnx->Core);
1189 IPRT_LINUX_RESTORE_EFL_AC();
1190 return rc;
1191}
1192
1193
1194DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
1195{
1196 IPRT_LINUX_SAVE_EFL_AC();
1197 void *pvLast = (uint8_t *)pv + cb - 1;
1198 size_t const cPages = cb >> PAGE_SHIFT;
1199 PRTR0MEMOBJLNX pMemLnx;
1200 bool fLinearMapping;
1201 int rc;
1202 uint8_t *pbPage;
1203 size_t iPage;
1204 NOREF(fAccess);
1205
1206 if ( !RTR0MemKernelIsValidAddr(pv)
1207 || !RTR0MemKernelIsValidAddr(pv + cb))
1208 return VERR_INVALID_PARAMETER;
1209
1210 /*
1211 * The lower part of the kernel memory has a linear mapping between
1212 * physical and virtual addresses. So we take a short cut here. This is
1213 * assumed to be the cleanest way to handle those addresses (and the code
1214 * is well tested, though the test for determining it is not very nice).
1215 * If we ever decide it isn't we can still remove it.
1216 */
1217#if 0
1218 fLinearMapping = (unsigned long)pvLast < VMALLOC_START;
1219#else
1220 fLinearMapping = (unsigned long)pv >= (unsigned long)__va(0)
1221 && (unsigned long)pvLast < (unsigned long)high_memory;
1222#endif
1223
1224 /*
1225 * Allocate the memory object.
1226 */
1227 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, pv, cb);
1228 if (!pMemLnx)
1229 {
1230 IPRT_LINUX_RESTORE_EFL_AC();
1231 return VERR_NO_MEMORY;
1232 }
1233
1234 /*
1235 * Gather the pages.
1236 * We ASSUME all kernel pages are non-swappable and non-movable.
1237 */
1238 rc = VINF_SUCCESS;
1239 pbPage = (uint8_t *)pvLast;
1240 iPage = cPages;
1241 if (!fLinearMapping)
1242 {
1243 while (iPage-- > 0)
1244 {
1245 struct page *pPage = rtR0MemObjLinuxVirtToPage(pbPage);
1246 if (RT_UNLIKELY(!pPage))
1247 {
1248 rc = VERR_LOCK_FAILED;
1249 break;
1250 }
1251 pMemLnx->apPages[iPage] = pPage;
1252 pbPage -= PAGE_SIZE;
1253 }
1254 }
1255 else
1256 {
1257 while (iPage-- > 0)
1258 {
1259 pMemLnx->apPages[iPage] = virt_to_page(pbPage);
1260 pbPage -= PAGE_SIZE;
1261 }
1262 }
1263 if (RT_SUCCESS(rc))
1264 {
1265 /*
1266 * Complete the memory object and return.
1267 */
1268 pMemLnx->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
1269 pMemLnx->cPages = cPages;
1270 Assert(!pMemLnx->fMappedToRing0);
1271 *ppMem = &pMemLnx->Core;
1272
1273 IPRT_LINUX_RESTORE_EFL_AC();
1274 return VINF_SUCCESS;
1275 }
1276
1277 rtR0MemObjDelete(&pMemLnx->Core);
1278 IPRT_LINUX_RESTORE_EFL_AC();
1279 return rc;
1280}
1281
1282
1283DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
1284{
1285#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1286 IPRT_LINUX_SAVE_EFL_AC();
1287 const size_t cPages = cb >> PAGE_SHIFT;
1288 struct page *pDummyPage;
1289 struct page **papPages;
1290
1291 /* check for unsupported stuff. */
1292 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
1293 if (uAlignment > PAGE_SIZE)
1294 return VERR_NOT_SUPPORTED;
1295
1296 /*
1297 * Allocate a dummy page and create a page pointer array for vmap such that
1298 * the dummy page is mapped all over the reserved area.
1299 */
1300 pDummyPage = alloc_page(GFP_HIGHUSER | __GFP_NOWARN);
1301 if (pDummyPage)
1302 {
1303 papPages = RTMemAlloc(sizeof(*papPages) * cPages);
1304 if (papPages)
1305 {
1306 void *pv;
1307 size_t iPage = cPages;
1308 while (iPage-- > 0)
1309 papPages[iPage] = pDummyPage;
1310# ifdef VM_MAP
1311 pv = vmap(papPages, cPages, VM_MAP, PAGE_KERNEL_RO);
1312# else
1313 pv = vmap(papPages, cPages, VM_ALLOC, PAGE_KERNEL_RO);
1314# endif
1315 RTMemFree(papPages);
1316 if (pv)
1317 {
1318 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
1319 if (pMemLnx)
1320 {
1321 pMemLnx->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
1322 pMemLnx->cPages = 1;
1323 pMemLnx->apPages[0] = pDummyPage;
1324 *ppMem = &pMemLnx->Core;
1325 IPRT_LINUX_RESTORE_EFL_AC();
1326 return VINF_SUCCESS;
1327 }
1328 vunmap(pv);
1329 }
1330 }
1331 __free_page(pDummyPage);
1332 }
1333 IPRT_LINUX_RESTORE_EFL_AC();
1334 return VERR_NO_MEMORY;
1335
1336#else /* < 2.4.22 */
1337 /*
1338 * Could probably use ioremap here, but the caller is in a better position than us
1339 * to select some safe physical memory.
1340 */
1341 return VERR_NOT_SUPPORTED;
1342#endif
1343}
1344
1345
1346DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
1347{
1348 IPRT_LINUX_SAVE_EFL_AC();
1349 PRTR0MEMOBJLNX pMemLnx;
1350 void *pv;
1351 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1352 if (!pTask)
1353 return VERR_NOT_SUPPORTED;
1354
1355 /*
1356 * Check that the specified alignment is supported.
1357 */
1358 if (uAlignment > PAGE_SIZE)
1359 return VERR_NOT_SUPPORTED;
1360
1361 /*
1362 * Let rtR0MemObjLinuxDoMmap do the difficult bits.
1363 */
1364 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cb, uAlignment, pTask, RTMEM_PROT_NONE);
1365 if (pv == (void *)-1)
1366 {
1367 IPRT_LINUX_RESTORE_EFL_AC();
1368 return VERR_NO_MEMORY;
1369 }
1370
1371 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
1372 if (!pMemLnx)
1373 {
1374 rtR0MemObjLinuxDoMunmap(pv, cb, pTask);
1375 IPRT_LINUX_RESTORE_EFL_AC();
1376 return VERR_NO_MEMORY;
1377 }
1378
1379 pMemLnx->Core.u.ResVirt.R0Process = R0Process;
1380 *ppMem = &pMemLnx->Core;
1381 IPRT_LINUX_RESTORE_EFL_AC();
1382 return VINF_SUCCESS;
1383}
1384
1385
1386DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap,
1387 void *pvFixed, size_t uAlignment,
1388 unsigned fProt, size_t offSub, size_t cbSub)
1389{
1390 int rc = VERR_NO_MEMORY;
1391 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1392 PRTR0MEMOBJLNX pMemLnx;
1393 IPRT_LINUX_SAVE_EFL_AC();
1394
1395 /* Fail if requested to do something we can't. */
1396 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
1397 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
1398 if (uAlignment > PAGE_SIZE)
1399 return VERR_NOT_SUPPORTED;
1400
1401 /*
1402 * Create the IPRT memory object.
1403 */
1404 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1405 if (pMemLnx)
1406 {
1407 if (pMemLnxToMap->cPages)
1408 {
1409#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1410 /*
1411 * Use vmap - 2.4.22 and later.
1412 */
1413 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, true /* kernel */);
1414# ifdef VM_MAP
1415 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_MAP, fPg);
1416# else
1417 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_ALLOC, fPg);
1418# endif
1419 if (pMemLnx->Core.pv)
1420 {
1421 pMemLnx->fMappedToRing0 = true;
1422 rc = VINF_SUCCESS;
1423 }
1424 else
1425 rc = VERR_MAP_FAILED;
1426
1427#else /* < 2.4.22 */
1428 /*
1429 * Only option here is to share mappings if possible and forget about fProt.
1430 */
1431 if (rtR0MemObjIsRing3(pMemToMap))
1432 rc = VERR_NOT_SUPPORTED;
1433 else
1434 {
1435 rc = VINF_SUCCESS;
1436 if (!pMemLnxToMap->Core.pv)
1437 rc = rtR0MemObjLinuxVMap(pMemLnxToMap, !!(fProt & RTMEM_PROT_EXEC));
1438 if (RT_SUCCESS(rc))
1439 {
1440 Assert(pMemLnxToMap->Core.pv);
1441 pMemLnx->Core.pv = pMemLnxToMap->Core.pv;
1442 }
1443 }
1444#endif
1445 }
1446 else
1447 {
1448 /*
1449 * MMIO / physical memory.
1450 */
1451 Assert(pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS && !pMemLnxToMap->Core.u.Phys.fAllocated);
1452 pMemLnx->Core.pv = pMemLnxToMap->Core.u.Phys.uCachePolicy == RTMEM_CACHE_POLICY_MMIO
1453 ? ioremap_nocache(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb)
1454 : ioremap(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb);
1455 if (pMemLnx->Core.pv)
1456 {
1457 /** @todo fix protection. */
1458 rc = VINF_SUCCESS;
1459 }
1460 }
1461 if (RT_SUCCESS(rc))
1462 {
1463 pMemLnx->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
1464 *ppMem = &pMemLnx->Core;
1465 IPRT_LINUX_RESTORE_EFL_AC();
1466 return VINF_SUCCESS;
1467 }
1468 rtR0MemObjDelete(&pMemLnx->Core);
1469 }
1470
1471 IPRT_LINUX_RESTORE_EFL_AC();
1472 return rc;
1473}
1474
1475
1476#ifdef VBOX_USE_PAE_HACK
1477/**
1478 * Replace the PFN of a PTE with the address of the actual page.
1479 *
1480 * The caller maps a reserved dummy page at the address with the desired access
1481 * and flags.
1482 *
1483 * This hack is required for older Linux kernels which don't provide
1484 * remap_pfn_range().
1485 *
1486 * @returns 0 on success, -ENOMEM on failure.
1487 * @param mm The memory context.
1488 * @param ulAddr The mapping address.
1489 * @param Phys The physical address of the page to map.
1490 */
1491static int rtR0MemObjLinuxFixPte(struct mm_struct *mm, unsigned long ulAddr, RTHCPHYS Phys)
1492{
1493 int rc = -ENOMEM;
1494 pgd_t *pgd;
1495
1496 spin_lock(&mm->page_table_lock);
1497
1498 pgd = pgd_offset(mm, ulAddr);
1499 if (!pgd_none(*pgd) && !pgd_bad(*pgd))
1500 {
1501 pmd_t *pmd = pmd_offset(pgd, ulAddr);
1502 if (!pmd_none(*pmd))
1503 {
1504 pte_t *ptep = pte_offset_map(pmd, ulAddr);
1505 if (ptep)
1506 {
1507 pte_t pte = *ptep;
1508 pte.pte_high &= 0xfff00000;
1509 pte.pte_high |= ((Phys >> 32) & 0x000fffff);
1510 pte.pte_low &= 0x00000fff;
1511 pte.pte_low |= (Phys & 0xfffff000);
1512 set_pte(ptep, pte);
1513 pte_unmap(ptep);
1514 rc = 0;
1515 }
1516 }
1517 }
1518
1519 spin_unlock(&mm->page_table_lock);
1520 return rc;
1521}
1522#endif /* VBOX_USE_PAE_HACK */
1523
1524
1525DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed,
1526 size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
1527{
1528 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1529 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1530 int rc = VERR_NO_MEMORY;
1531 PRTR0MEMOBJLNX pMemLnx;
1532#ifdef VBOX_USE_PAE_HACK
1533 struct page *pDummyPage;
1534 RTHCPHYS DummyPhys;
1535#endif
1536 IPRT_LINUX_SAVE_EFL_AC();
1537
1538 /*
1539 * Check for restrictions.
1540 */
1541 if (!pTask)
1542 return VERR_NOT_SUPPORTED;
1543 if (uAlignment > PAGE_SIZE)
1544 return VERR_NOT_SUPPORTED;
1545
1546#ifdef VBOX_USE_PAE_HACK
1547 /*
1548 * Allocate a dummy page for use when mapping the memory.
1549 */
1550 pDummyPage = alloc_page(GFP_USER | __GFP_NOWARN);
1551 if (!pDummyPage)
1552 {
1553 IPRT_LINUX_RESTORE_EFL_AC();
1554 return VERR_NO_MEMORY;
1555 }
1556 SetPageReserved(pDummyPage);
1557 DummyPhys = page_to_phys(pDummyPage);
1558#endif
1559
1560 /*
1561 * Create the IPRT memory object.
1562 */
1563 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1564 if (pMemLnx)
1565 {
1566 /*
1567 * Allocate user space mapping.
1568 */
1569 void *pv;
1570 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, pMemLnxToMap->Core.cb, uAlignment, pTask, fProt);
1571 if (pv != (void *)-1)
1572 {
1573 /*
1574 * Map page by page into the mmap area.
1575 * This is generic, paranoid and not very efficient.
1576 */
1577 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, false /* user */);
1578 unsigned long ulAddrCur = (unsigned long)pv;
1579 const size_t cPages = pMemLnxToMap->Core.cb >> PAGE_SHIFT;
1580 size_t iPage;
1581
1582 down_write(&pTask->mm->mmap_sem);
1583
1584 rc = VINF_SUCCESS;
1585 if (pMemLnxToMap->cPages)
1586 {
1587 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
1588 {
1589#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
1590 RTHCPHYS Phys = page_to_phys(pMemLnxToMap->apPages[iPage]);
1591#endif
1592#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1593 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1594 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1595#endif
1596#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_X86)
1597 /* remap_page_range() limitation on x86 */
1598 AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
1599#endif
1600
1601#if defined(VBOX_USE_INSERT_PAGE) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
1602 rc = vm_insert_page(vma, ulAddrCur, pMemLnxToMap->apPages[iPage]);
1603 /* Thes flags help making 100% sure some bad stuff wont happen (swap, core, ++).
1604 * See remap_pfn_range() in mm/memory.c */
1605#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
1606 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1607#else
1608 vma->vm_flags |= VM_RESERVED;
1609#endif
1610#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1611 rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1612#elif defined(VBOX_USE_PAE_HACK)
1613 rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
1614 if (!rc)
1615 rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
1616#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1617 rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1618#else /* 2.4 */
1619 rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
1620#endif
1621 if (rc)
1622 {
1623 rc = VERR_NO_MEMORY;
1624 break;
1625 }
1626 }
1627 }
1628 else
1629 {
1630 RTHCPHYS Phys;
1631 if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS)
1632 Phys = pMemLnxToMap->Core.u.Phys.PhysBase;
1633 else if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_CONT)
1634 Phys = pMemLnxToMap->Core.u.Cont.Phys;
1635 else
1636 {
1637 AssertMsgFailed(("%d\n", pMemLnxToMap->Core.enmType));
1638 Phys = NIL_RTHCPHYS;
1639 }
1640 if (Phys != NIL_RTHCPHYS)
1641 {
1642 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE, Phys += PAGE_SIZE)
1643 {
1644#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1645 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1646 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1647#endif
1648#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_X86)
1649 /* remap_page_range() limitation on x86 */
1650 AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
1651#endif
1652
1653#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1654 rc = remap_pfn_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1655#elif defined(VBOX_USE_PAE_HACK)
1656 rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
1657 if (!rc)
1658 rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
1659#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1660 rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1661#else /* 2.4 */
1662 rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
1663#endif
1664 if (rc)
1665 {
1666 rc = VERR_NO_MEMORY;
1667 break;
1668 }
1669 }
1670 }
1671 }
1672
1673#ifdef CONFIG_NUMA_BALANCING
1674# if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
1675# ifdef RHEL_RELEASE_CODE
1676# if RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(7, 0)
1677# define VBOX_NUMA_HACK_OLD
1678# endif
1679# endif
1680# endif
1681 if (RT_SUCCESS(rc))
1682 {
1683 /** @todo Ugly hack! But right now we have no other means to
1684 * disable automatic NUMA page balancing. */
1685# ifdef RT_OS_X86
1686# ifdef VBOX_NUMA_HACK_OLD
1687 pTask->mm->numa_next_reset = jiffies + 0x7fffffffUL;
1688# endif
1689 pTask->mm->numa_next_scan = jiffies + 0x7fffffffUL;
1690# else
1691# ifdef VBOX_NUMA_HACK_OLD
1692 pTask->mm->numa_next_reset = jiffies + 0x7fffffffffffffffUL;
1693# endif
1694 pTask->mm->numa_next_scan = jiffies + 0x7fffffffffffffffUL;
1695# endif
1696 }
1697#endif /* CONFIG_NUMA_BALANCING */
1698
1699 up_write(&pTask->mm->mmap_sem);
1700
1701 if (RT_SUCCESS(rc))
1702 {
1703#ifdef VBOX_USE_PAE_HACK
1704 __free_page(pDummyPage);
1705#endif
1706 pMemLnx->Core.pv = pv;
1707 pMemLnx->Core.u.Mapping.R0Process = R0Process;
1708 *ppMem = &pMemLnx->Core;
1709 IPRT_LINUX_RESTORE_EFL_AC();
1710 return VINF_SUCCESS;
1711 }
1712
1713 /*
1714 * Bail out.
1715 */
1716 rtR0MemObjLinuxDoMunmap(pv, pMemLnxToMap->Core.cb, pTask);
1717 }
1718 rtR0MemObjDelete(&pMemLnx->Core);
1719 }
1720#ifdef VBOX_USE_PAE_HACK
1721 __free_page(pDummyPage);
1722#endif
1723
1724 IPRT_LINUX_RESTORE_EFL_AC();
1725 return rc;
1726}
1727
1728
1729DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
1730{
1731 NOREF(pMem);
1732 NOREF(offSub);
1733 NOREF(cbSub);
1734 NOREF(fProt);
1735 return VERR_NOT_SUPPORTED;
1736}
1737
1738
1739DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
1740{
1741 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
1742
1743 if (pMemLnx->cPages)
1744 return page_to_phys(pMemLnx->apPages[iPage]);
1745
1746 switch (pMemLnx->Core.enmType)
1747 {
1748 case RTR0MEMOBJTYPE_CONT:
1749 return pMemLnx->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
1750
1751 case RTR0MEMOBJTYPE_PHYS:
1752 return pMemLnx->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
1753
1754 /* the parent knows */
1755 case RTR0MEMOBJTYPE_MAPPING:
1756 return rtR0MemObjNativeGetPagePhysAddr(pMemLnx->Core.uRel.Child.pParent, iPage);
1757
1758 /* cPages > 0 */
1759 case RTR0MEMOBJTYPE_LOW:
1760 case RTR0MEMOBJTYPE_LOCK:
1761 case RTR0MEMOBJTYPE_PHYS_NC:
1762 case RTR0MEMOBJTYPE_PAGE:
1763 default:
1764 AssertMsgFailed(("%d\n", pMemLnx->Core.enmType));
1765 /* fall thru */
1766
1767 case RTR0MEMOBJTYPE_RES_VIRT:
1768 return NIL_RTHCPHYS;
1769 }
1770}
1771
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