VirtualBox

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

Last change on this file since 96445 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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

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