VirtualBox

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

Last change on this file since 22436 was 22355, checked in by vboxsync, 16 years ago

Runtime/Linux: don't allow to pass pages above 4GB to remap_page_range(); some remarks about GFP_USER

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev
File size: 41.5 KB
Line 
1/* $Revision: 22355 $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-linux-kernel.h"
36
37#include <iprt/memobj.h>
38#include <iprt/alloc.h>
39#include <iprt/assert.h>
40#include <iprt/log.h>
41#include <iprt/process.h>
42#include <iprt/string.h>
43#include "internal/memobj.h"
44
45
46/*******************************************************************************
47* Defined Constants And Macros *
48*******************************************************************************/
49/* early 2.6 kernels */
50#ifndef PAGE_SHARED_EXEC
51# define PAGE_SHARED_EXEC PAGE_SHARED
52#endif
53#ifndef PAGE_READONLY_EXEC
54# define PAGE_READONLY_EXEC PAGE_READONLY
55#endif
56
57/*
58 * 2.6.29+ kernels don't work with remap_pfn_range() anymore because
59 * track_pfn_vma_new() is apparently not defined for non-RAM pages.
60 * It should be safe to use vm_insert_page() older kernels as well.
61 */
62#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
63# define VBOX_USE_INSERT_PAGE
64#endif
65
66
67/*******************************************************************************
68* Structures and Typedefs *
69*******************************************************************************/
70/**
71 * The Darwin version of the memory object structure.
72 */
73typedef struct RTR0MEMOBJLNX
74{
75 /** The core structure. */
76 RTR0MEMOBJINTERNAL Core;
77 /** Set if the allocation is contiguous.
78 * This means it has to be given back as one chunk. */
79 bool fContiguous;
80 /** Set if we've vmap'ed thed memory into ring-0. */
81 bool fMappedToRing0;
82 /** The pages in the apPages array. */
83 size_t cPages;
84 /** Array of struct page pointers. (variable size) */
85 struct page *apPages[1];
86} RTR0MEMOBJLNX, *PRTR0MEMOBJLNX;
87
88
89/**
90 * Helper that converts from a RTR0PROCESS handle to a linux task.
91 *
92 * @returns The corresponding Linux task.
93 * @param R0Process IPRT ring-0 process handle.
94 */
95struct task_struct *rtR0ProcessToLinuxTask(RTR0PROCESS R0Process)
96{
97 /** @todo fix rtR0ProcessToLinuxTask!! */
98 return R0Process == RTR0ProcHandleSelf() ? current : NULL;
99}
100
101
102/**
103 * Compute order. Some functions allocate 2^order pages.
104 *
105 * @returns order.
106 * @param cPages Number of pages.
107 */
108static int rtR0MemObjLinuxOrder(size_t cPages)
109{
110 int iOrder;
111 size_t cTmp;
112
113 for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
114 ;
115 if (cPages & ~((size_t)1 << iOrder))
116 ++iOrder;
117
118 return iOrder;
119}
120
121
122/**
123 * Converts from RTMEM_PROT_* to Linux PAGE_*.
124 *
125 * @returns Linux page protection constant.
126 * @param fProt The IPRT protection mask.
127 * @param fKernel Whether it applies to kernel or user space.
128 */
129static pgprot_t rtR0MemObjLinuxConvertProt(unsigned fProt, bool fKernel)
130{
131 switch (fProt)
132 {
133 default:
134 AssertMsgFailed(("%#x %d\n", fProt, fKernel));
135 case RTMEM_PROT_NONE:
136 return PAGE_NONE;
137
138 case RTMEM_PROT_READ:
139 return fKernel ? PAGE_KERNEL_RO : PAGE_READONLY;
140
141 case RTMEM_PROT_WRITE:
142 case RTMEM_PROT_WRITE | RTMEM_PROT_READ:
143 return fKernel ? PAGE_KERNEL : PAGE_SHARED;
144
145 case RTMEM_PROT_EXEC:
146 case RTMEM_PROT_EXEC | RTMEM_PROT_READ:
147#if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
148 if (fKernel)
149 {
150 pgprot_t fPg = MY_PAGE_KERNEL_EXEC;
151 pgprot_val(fPg) &= ~_PAGE_RW;
152 return fPg;
153 }
154 return PAGE_READONLY_EXEC;
155#else
156 return fKernel ? MY_PAGE_KERNEL_EXEC : PAGE_READONLY_EXEC;
157#endif
158
159 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
160 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_READ:
161 return fKernel ? MY_PAGE_KERNEL_EXEC : PAGE_SHARED_EXEC;
162 }
163}
164
165
166/**
167 * Internal worker that allocates physical pages and creates the memory object for them.
168 *
169 * @returns IPRT status code.
170 * @param ppMemLnx Where to store the memory object pointer.
171 * @param enmType The object type.
172 * @param cb The number of bytes to allocate.
173 * @param fFlagsLnx The page allocation flags (GPFs).
174 * @param fContiguous Whether the allocation must be contiguous.
175 */
176static int rtR0MemObjLinuxAllocPages(PRTR0MEMOBJLNX *ppMemLnx, RTR0MEMOBJTYPE enmType, size_t cb, unsigned fFlagsLnx, bool fContiguous)
177{
178 size_t iPage;
179 size_t cPages = cb >> PAGE_SHIFT;
180 struct page *paPages;
181
182 /*
183 * Allocate a memory object structure that's large enough to contain
184 * the page pointer array.
185 */
186 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), enmType, NULL, cb);
187 if (!pMemLnx)
188 return VERR_NO_MEMORY;
189 pMemLnx->cPages = cPages;
190
191 /*
192 * Allocate the pages.
193 * For small allocations we'll try contiguous first and then fall back on page by page.
194 */
195#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
196 if ( fContiguous
197 || cb <= PAGE_SIZE * 2)
198 {
199#ifdef VBOX_USE_INSERT_PAGE
200 paPages = alloc_pages(fFlagsLnx | __GFP_COMP, rtR0MemObjLinuxOrder(cb >> PAGE_SHIFT));
201#else
202 paPages = alloc_pages(fFlagsLnx, rtR0MemObjLinuxOrder(cb >> PAGE_SHIFT));
203#endif
204 if (paPages)
205 {
206 fContiguous = true;
207 for (iPage = 0; iPage < cPages; iPage++)
208 pMemLnx->apPages[iPage] = &paPages[iPage];
209 }
210 else if (fContiguous)
211 {
212 rtR0MemObjDelete(&pMemLnx->Core);
213 return VERR_NO_MEMORY;
214 }
215 }
216
217 if (!fContiguous)
218 {
219 for (iPage = 0; iPage < cPages; iPage++)
220 {
221 pMemLnx->apPages[iPage] = alloc_page(fFlagsLnx);
222 if (RT_UNLIKELY(!pMemLnx->apPages[iPage]))
223 {
224 while (iPage-- > 0)
225 __free_page(pMemLnx->apPages[iPage]);
226 rtR0MemObjDelete(&pMemLnx->Core);
227 return VERR_NO_MEMORY;
228 }
229 }
230 }
231
232#else /* < 2.4.22 */
233 /** @todo figure out why we didn't allocate page-by-page on 2.4.21 and older... */
234 paPages = alloc_pages(fFlagsLnx, rtR0MemObjLinuxOrder(cb >> PAGE_SHIFT));
235 if (!paPages)
236 {
237 rtR0MemObjDelete(&pMemLnx->Core);
238 return VERR_NO_MEMORY;
239 }
240 for (iPage = 0; iPage < cPages; iPage++)
241 {
242 pMemLnx->apPages[iPage] = &paPages[iPage];
243 MY_SET_PAGES_EXEC(pMemLnx->apPages[iPage], 1);
244 if (PageHighMem(pMemLnx->apPages[iPage]))
245 BUG();
246 }
247
248 fContiguous = true;
249#endif /* < 2.4.22 */
250 pMemLnx->fContiguous = fContiguous;
251
252 /*
253 * Reserve the pages.
254 */
255 for (iPage = 0; iPage < cPages; iPage++)
256 SetPageReserved(pMemLnx->apPages[iPage]);
257
258 *ppMemLnx = pMemLnx;
259 return VINF_SUCCESS;
260}
261
262
263/**
264 * Frees the physical pages allocated by the rtR0MemObjLinuxAllocPages() call.
265 *
266 * This method does NOT free the object.
267 *
268 * @param pMemLnx The object which physical pages should be freed.
269 */
270static void rtR0MemObjLinuxFreePages(PRTR0MEMOBJLNX pMemLnx)
271{
272 size_t iPage = pMemLnx->cPages;
273 if (iPage > 0)
274 {
275 /*
276 * Restore the page flags.
277 */
278 while (iPage-- > 0)
279 {
280 ClearPageReserved(pMemLnx->apPages[iPage]);
281#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
282#else
283 MY_SET_PAGES_NOEXEC(pMemLnx->apPages[iPage], 1);
284#endif
285 }
286
287 /*
288 * Free the pages.
289 */
290#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
291 if (!pMemLnx->fContiguous)
292 {
293 iPage = pMemLnx->cPages;
294 while (iPage-- > 0)
295 __free_page(pMemLnx->apPages[iPage]);
296 }
297 else
298#endif
299 __free_pages(pMemLnx->apPages[0], rtR0MemObjLinuxOrder(pMemLnx->cPages));
300
301 pMemLnx->cPages = 0;
302 }
303}
304
305
306/**
307 * Maps the allocation into ring-0.
308 *
309 * This will update the RTR0MEMOBJLNX::Core.pv and RTR0MEMOBJ::fMappedToRing0 members.
310 *
311 * Contiguous mappings that isn't in 'high' memory will already be mapped into kernel
312 * space, so we'll use that mapping if possible. If execute access is required, we'll
313 * play safe and do our own mapping.
314 *
315 * @returns IPRT status code.
316 * @param pMemLnx The linux memory object to map.
317 * @param fExecutable Whether execute access is required.
318 */
319static int rtR0MemObjLinuxVMap(PRTR0MEMOBJLNX pMemLnx, bool fExecutable)
320{
321 int rc = VINF_SUCCESS;
322
323 /*
324 * Choose mapping strategy.
325 */
326 bool fMustMap = fExecutable
327 || !pMemLnx->fContiguous;
328 if (!fMustMap)
329 {
330 size_t iPage = pMemLnx->cPages;
331 while (iPage-- > 0)
332 if (PageHighMem(pMemLnx->apPages[iPage]))
333 {
334 fMustMap = true;
335 break;
336 }
337 }
338
339 Assert(!pMemLnx->Core.pv);
340 Assert(!pMemLnx->fMappedToRing0);
341
342 if (fMustMap)
343 {
344 /*
345 * Use vmap - 2.4.22 and later.
346 */
347#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
348 pgprot_t fPg;
349 pgprot_val(fPg) = _PAGE_PRESENT | _PAGE_RW;
350# ifdef _PAGE_NX
351 if (!fExecutable)
352 pgprot_val(fPg) |= _PAGE_NX;
353# endif
354
355# ifdef VM_MAP
356 pMemLnx->Core.pv = vmap(&pMemLnx->apPages[0], pMemLnx->cPages, VM_MAP, fPg);
357# else
358 pMemLnx->Core.pv = vmap(&pMemLnx->apPages[0], pMemLnx->cPages, VM_ALLOC, fPg);
359# endif
360 if (pMemLnx->Core.pv)
361 pMemLnx->fMappedToRing0 = true;
362 else
363 rc = VERR_MAP_FAILED;
364#else /* < 2.4.22 */
365 rc = VERR_NOT_SUPPORTED;
366#endif
367 }
368 else
369 {
370 /*
371 * Use the kernel RAM mapping.
372 */
373 pMemLnx->Core.pv = phys_to_virt(page_to_phys(pMemLnx->apPages[0]));
374 Assert(pMemLnx->Core.pv);
375 }
376
377 return rc;
378}
379
380
381/**
382 * Undos what rtR0MemObjLinuxVMap() did.
383 *
384 * @param pMemLnx The linux memory object.
385 */
386static void rtR0MemObjLinuxVUnmap(PRTR0MEMOBJLNX pMemLnx)
387{
388#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
389 if (pMemLnx->fMappedToRing0)
390 {
391 Assert(pMemLnx->Core.pv);
392 vunmap(pMemLnx->Core.pv);
393 pMemLnx->fMappedToRing0 = false;
394 }
395#else /* < 2.4.22 */
396 Assert(!pMemLnx->fMappedToRing0);
397#endif
398 pMemLnx->Core.pv = NULL;
399}
400
401
402int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
403{
404 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
405
406 /*
407 * Release any memory that we've allocated or locked.
408 */
409 switch (pMemLnx->Core.enmType)
410 {
411 case RTR0MEMOBJTYPE_LOW:
412 case RTR0MEMOBJTYPE_PAGE:
413 case RTR0MEMOBJTYPE_CONT:
414 case RTR0MEMOBJTYPE_PHYS:
415 case RTR0MEMOBJTYPE_PHYS_NC:
416 rtR0MemObjLinuxVUnmap(pMemLnx);
417 rtR0MemObjLinuxFreePages(pMemLnx);
418 break;
419
420 case RTR0MEMOBJTYPE_LOCK:
421 if (pMemLnx->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
422 {
423 struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
424 size_t iPage;
425 Assert(pTask);
426 if (pTask && pTask->mm)
427 down_read(&pTask->mm->mmap_sem);
428
429 iPage = pMemLnx->cPages;
430 while (iPage-- > 0)
431 {
432 if (!PageReserved(pMemLnx->apPages[iPage]))
433 SetPageDirty(pMemLnx->apPages[iPage]);
434 page_cache_release(pMemLnx->apPages[iPage]);
435 }
436
437 if (pTask && pTask->mm)
438 up_read(&pTask->mm->mmap_sem);
439 }
440 /* else: kernel memory - nothing to do here. */
441 break;
442
443 case RTR0MEMOBJTYPE_RES_VIRT:
444 Assert(pMemLnx->Core.pv);
445 if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
446 {
447 struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
448 Assert(pTask);
449 if (pTask && pTask->mm)
450 {
451 down_write(&pTask->mm->mmap_sem);
452 MY_DO_MUNMAP(pTask->mm, (unsigned long)pMemLnx->Core.pv, pMemLnx->Core.cb);
453 up_write(&pTask->mm->mmap_sem);
454 }
455 }
456 else
457 {
458 vunmap(pMemLnx->Core.pv);
459
460 Assert(pMemLnx->cPages == 1 && pMemLnx->apPages[0] != NULL);
461 __free_page(pMemLnx->apPages[0]);
462 pMemLnx->apPages[0] = NULL;
463 pMemLnx->cPages = 0;
464 }
465 pMemLnx->Core.pv = NULL;
466 break;
467
468 case RTR0MEMOBJTYPE_MAPPING:
469 Assert(pMemLnx->cPages == 0); Assert(pMemLnx->Core.pv);
470 if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
471 {
472 struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
473 Assert(pTask);
474 if (pTask && pTask->mm)
475 {
476 down_write(&pTask->mm->mmap_sem);
477 MY_DO_MUNMAP(pTask->mm, (unsigned long)pMemLnx->Core.pv, pMemLnx->Core.cb);
478 up_write(&pTask->mm->mmap_sem);
479 }
480 }
481 else
482 vunmap(pMemLnx->Core.pv);
483 pMemLnx->Core.pv = NULL;
484 break;
485
486 default:
487 AssertMsgFailed(("enmType=%d\n", pMemLnx->Core.enmType));
488 return VERR_INTERNAL_ERROR;
489 }
490 return VINF_SUCCESS;
491}
492
493
494int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
495{
496 PRTR0MEMOBJLNX pMemLnx;
497 int rc;
498
499#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
500 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, GFP_HIGHUSER, false /* non-contiguous */);
501#else
502 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, GFP_USER, false /* non-contiguous */);
503#endif
504 if (RT_SUCCESS(rc))
505 {
506 rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
507 if (RT_SUCCESS(rc))
508 {
509 *ppMem = &pMemLnx->Core;
510 return rc;
511 }
512
513 rtR0MemObjLinuxFreePages(pMemLnx);
514 rtR0MemObjDelete(&pMemLnx->Core);
515 }
516
517 return rc;
518}
519
520
521int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
522{
523 PRTR0MEMOBJLNX pMemLnx;
524 int rc;
525
526#ifdef RT_ARCH_AMD64
527# ifdef GFP_DMA32
528 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, GFP_DMA32, false /* non-contiguous */);
529 if (RT_FAILURE(rc))
530# endif
531 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, GFP_DMA, false /* non-contiguous */);
532#else
533 /** XXX Wrong: GFP_USER can return page frames above 4GB! */
534 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, GFP_USER, false /* non-contiguous */);
535#endif
536 if (RT_SUCCESS(rc))
537 {
538 rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
539 if (RT_SUCCESS(rc))
540 {
541 *ppMem = &pMemLnx->Core;
542 return rc;
543 }
544
545 rtR0MemObjLinuxFreePages(pMemLnx);
546 rtR0MemObjDelete(&pMemLnx->Core);
547 }
548
549 return rc;
550}
551
552
553int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
554{
555 PRTR0MEMOBJLNX pMemLnx;
556 int rc;
557
558#ifdef RT_ARCH_AMD64
559# ifdef GFP_DMA32
560 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, GFP_DMA32, true /* contiguous */);
561 if (RT_FAILURE(rc))
562# endif
563 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, GFP_DMA, true /* contiguous */);
564#else
565 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, GFP_USER, true /* contiguous */);
566#endif
567 if (RT_SUCCESS(rc))
568 {
569 rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
570 if (RT_SUCCESS(rc))
571 {
572#if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(CONFIG_HIGHMEM64G))
573 size_t iPage = pMemLnx->cPages;
574 while (iPage-- > 0)
575 Assert(page_to_phys(pMemLnx->apPages[iPage]) < _4G);
576#endif
577 pMemLnx->Core.u.Cont.Phys = page_to_phys(pMemLnx->apPages[0]);
578 *ppMem = &pMemLnx->Core;
579 return rc;
580 }
581
582 rtR0MemObjLinuxFreePages(pMemLnx);
583 rtR0MemObjDelete(&pMemLnx->Core);
584 }
585
586 return rc;
587}
588
589
590/**
591 * Worker for rtR0MemObjLinuxAllocPhysSub that tries one allocation strategy.
592 *
593 * @returns IPRT status.
594 * @param ppMemLnx Where to
595 * @param enmType The object type.
596 * @param cb The size of the allocation.
597 * @param PhysHighest See rtR0MemObjNativeAllocPhys.
598 * @param fGfp The Linux GFP flags to use for the allocation.
599 */
600static int rtR0MemObjLinuxAllocPhysSub2(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType, size_t cb, RTHCPHYS PhysHighest, unsigned fGfp)
601{
602 PRTR0MEMOBJLNX pMemLnx;
603 int rc;
604
605 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, enmType, cb, fGfp,
606 enmType == RTR0MEMOBJTYPE_PHYS /* contiguous / non-contiguous */);
607 if (RT_FAILURE(rc))
608 return rc;
609
610 /*
611 * Check the addresses if necessary. (Can be optimized a bit for PHYS.)
612 */
613 if (PhysHighest != NIL_RTHCPHYS)
614 {
615 size_t iPage = pMemLnx->cPages;
616 while (iPage-- > 0)
617 if (page_to_phys(pMemLnx->apPages[iPage]) >= PhysHighest)
618 {
619 rtR0MemObjLinuxFreePages(pMemLnx);
620 rtR0MemObjDelete(&pMemLnx->Core);
621 return VERR_NO_MEMORY;
622 }
623 }
624
625 /*
626 * Complete the object.
627 */
628 if (enmType == RTR0MEMOBJTYPE_PHYS)
629 {
630 pMemLnx->Core.u.Phys.PhysBase = page_to_phys(pMemLnx->apPages[0]);
631 pMemLnx->Core.u.Phys.fAllocated = true;
632 }
633 *ppMem = &pMemLnx->Core;
634 return rc;
635}
636
637
638/**
639 * Worker for rtR0MemObjNativeAllocPhys and rtR0MemObjNativeAllocPhysNC.
640 *
641 * @returns IPRT status.
642 * @param ppMem Where to store the memory object pointer on success.
643 * @param enmType The object type.
644 * @param cb The size of the allocation.
645 * @param PhysHighest See rtR0MemObjNativeAllocPhys.
646 */
647static int rtR0MemObjLinuxAllocPhysSub(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType, size_t cb, RTHCPHYS PhysHighest)
648{
649 int rc;
650
651 /*
652 * There are two clear cases and that's the <=16MB and anything-goes ones.
653 * When the physical address limit is somewhere inbetween those two we'll
654 * just have to try, starting with HIGHUSER and working our way thru the
655 * different types, hoping we'll get lucky.
656 *
657 * We should probably move this physical address restriction logic up to
658 * the page alloc function as it would be more efficient there. But since
659 * we don't expect this to be a performance issue just yet it can wait.
660 */
661 if (PhysHighest == NIL_RTHCPHYS)
662 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_HIGHUSER);
663 else if (PhysHighest <= _1M * 16)
664 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_DMA);
665 else
666 {
667 rc = VERR_NO_MEMORY;
668 if (RT_FAILURE(rc))
669 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_HIGHUSER);
670 if (RT_FAILURE(rc))
671 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_USER);
672#ifdef GFP_DMA32
673 if (RT_FAILURE(rc))
674 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_DMA32);
675#endif
676 if (RT_FAILURE(rc))
677 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_DMA);
678 }
679 return rc;
680}
681
682
683int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
684{
685 return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS, cb, PhysHighest);
686}
687
688
689int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
690{
691 return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS_NC, cb, PhysHighest);
692}
693
694
695int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
696{
697 /*
698 * All we need to do here is to validate that we can use
699 * ioremap on the specified address (32/64-bit dma_addr_t).
700 */
701 PRTR0MEMOBJLNX pMemLnx;
702 dma_addr_t PhysAddr = Phys;
703 AssertMsgReturn(PhysAddr == Phys, ("%#llx\n", (unsigned long long)Phys), VERR_ADDRESS_TOO_BIG);
704
705 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_PHYS, NULL, cb);
706 if (!pMemLnx)
707 return VERR_NO_MEMORY;
708
709 pMemLnx->Core.u.Phys.PhysBase = PhysAddr;
710 pMemLnx->Core.u.Phys.fAllocated = false;
711 Assert(!pMemLnx->cPages);
712 *ppMem = &pMemLnx->Core;
713 return VINF_SUCCESS;
714}
715
716
717int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
718{
719 const int cPages = cb >> PAGE_SHIFT;
720 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
721 struct vm_area_struct **papVMAs;
722 PRTR0MEMOBJLNX pMemLnx;
723 int rc = VERR_NO_MEMORY;
724
725 /*
726 * Check for valid task and size overflows.
727 */
728 if (!pTask)
729 return VERR_NOT_SUPPORTED;
730 if (((size_t)cPages << PAGE_SHIFT) != cb)
731 return VERR_OUT_OF_RANGE;
732
733 /*
734 * Allocate the memory object and a temporary buffer for the VMAs.
735 */
736 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
737 if (!pMemLnx)
738 return VERR_NO_MEMORY;
739
740 papVMAs = (struct vm_area_struct **)RTMemAlloc(sizeof(*papVMAs) * cPages);
741 if (papVMAs)
742 {
743 down_read(&pTask->mm->mmap_sem);
744
745 /*
746 * Get user pages.
747 */
748 rc = get_user_pages(pTask, /* Task for fault acounting. */
749 pTask->mm, /* Whose pages. */
750 R3Ptr, /* Where from. */
751 cPages, /* How many pages. */
752 1, /* Write to memory. */
753 0, /* force. */
754 &pMemLnx->apPages[0], /* Page array. */
755 papVMAs); /* vmas */
756 if (rc == cPages)
757 {
758 /*
759 * Flush dcache (required?), protect against fork and _really_ pin the page
760 * table entries. get_user_pages() will protect against swapping out the
761 * pages but it will NOT protect against removing page table entries. This
762 * can be achieved with
763 * - using mlock / mmap(..., MAP_LOCKED, ...) from userland. This requires
764 * an appropriate limit set up with setrlimit(..., RLIMIT_MEMLOCK, ...).
765 * Usual Linux distributions support only a limited size of locked pages
766 * (e.g. 32KB).
767 * - setting the PageReserved bit (as we do in rtR0MemObjLinuxAllocPages()
768 * or by
769 * - setting the VM_LOCKED flag. This is the same as doing mlock() without
770 * a range check.
771 */
772 /** @todo The Linux fork() protection will require more work if this API
773 * is to be used for anything but locking VM pages. */
774 while (rc-- > 0)
775 {
776 flush_dcache_page(pMemLnx->apPages[rc]);
777 papVMAs[rc]->vm_flags |= (VM_DONTCOPY | VM_LOCKED);
778 }
779
780 up_read(&pTask->mm->mmap_sem);
781
782 RTMemFree(papVMAs);
783
784 pMemLnx->Core.u.Lock.R0Process = R0Process;
785 pMemLnx->cPages = cPages;
786 Assert(!pMemLnx->fMappedToRing0);
787 *ppMem = &pMemLnx->Core;
788
789 return VINF_SUCCESS;
790 }
791
792 /*
793 * Failed - we need to unlock any pages that we succeeded to lock.
794 */
795 while (rc-- > 0)
796 {
797 if (!PageReserved(pMemLnx->apPages[rc]))
798 SetPageDirty(pMemLnx->apPages[rc]);
799 page_cache_release(pMemLnx->apPages[rc]);
800 }
801
802 up_read(&pTask->mm->mmap_sem);
803
804 RTMemFree(papVMAs);
805 rc = VERR_LOCK_FAILED;
806 }
807
808 rtR0MemObjDelete(&pMemLnx->Core);
809 return rc;
810}
811
812
813int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
814{
815 void *pvLast = (uint8_t *)pv + cb - 1;
816 size_t const cPages = cb >> PAGE_SHIFT;
817 PRTR0MEMOBJLNX pMemLnx;
818 bool fLinearMapping;
819 int rc;
820 uint8_t *pbPage;
821 size_t iPage;
822
823 /*
824 * Classify the memory and check that we can deal with it.
825 */
826#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
827 fLinearMapping = virt_addr_valid(pvLast) && virt_addr_valid(pv);
828#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0)
829 fLinearMapping = VALID_PAGE(virt_to_page(pvLast)) && VALID_PAGE(virt_to_page(pv));
830#else
831# error "not supported"
832#endif
833 if (!fLinearMapping)
834 {
835#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 19)
836 if ( !RTR0MemKernelIsValidAddr(pv)
837 || !RTR0MemKernelIsValidAddr(pv + cb))
838#endif
839 return VERR_INVALID_PARAMETER;
840 }
841
842 /*
843 * Allocate the memory object.
844 */
845 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, pv, cb);
846 if (!pMemLnx)
847 return VERR_NO_MEMORY;
848
849 /*
850 * Gather the pages.
851 * We ASSUME all kernel pages are non-swappable.
852 */
853 rc = VINF_SUCCESS;
854 pbPage = (uint8_t *)pvLast;
855 iPage = cPages;
856#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 19)
857 if (!fLinearMapping)
858 {
859 while (iPage-- > 0)
860 {
861 struct page *pPage = vmalloc_to_page(pbPage);
862 if (RT_UNLIKELY(!pPage))
863 {
864 rc = VERR_LOCK_FAILED;
865 break;
866 }
867 pMemLnx->apPages[iPage] = pPage;
868 pbPage -= PAGE_SIZE;
869 }
870 }
871 else
872#endif
873 {
874 while (iPage-- > 0)
875 {
876 pMemLnx->apPages[iPage] = virt_to_page(pbPage);
877 pbPage -= PAGE_SIZE;
878 }
879 }
880 if (RT_SUCCESS(rc))
881 {
882 /*
883 * Complete the memory object and return.
884 */
885 pMemLnx->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
886 pMemLnx->cPages = cPages;
887 Assert(!pMemLnx->fMappedToRing0);
888 *ppMem = &pMemLnx->Core;
889
890 return VINF_SUCCESS;
891 }
892
893 rtR0MemObjDelete(&pMemLnx->Core);
894 return rc;
895}
896
897
898int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
899{
900#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
901 const size_t cPages = cb >> PAGE_SHIFT;
902 struct page *pDummyPage;
903 struct page **papPages;
904
905 /* check for unsupported stuff. */
906 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
907 if (uAlignment > PAGE_SIZE)
908 return VERR_NOT_SUPPORTED;
909
910 /*
911 * Allocate a dummy page and create a page pointer array for vmap such that
912 * the dummy page is mapped all over the reserved area.
913 */
914 pDummyPage = alloc_page(GFP_HIGHUSER);
915 if (!pDummyPage)
916 return VERR_NO_MEMORY;
917 papPages = RTMemAlloc(sizeof(*papPages) * cPages);
918 if (papPages)
919 {
920 void *pv;
921 size_t iPage = cPages;
922 while (iPage-- > 0)
923 papPages[iPage] = pDummyPage;
924# ifdef VM_MAP
925 pv = vmap(papPages, cPages, VM_MAP, PAGE_KERNEL_RO);
926# else
927 pv = vmap(papPages, cPages, VM_ALLOC, PAGE_KERNEL_RO);
928# endif
929 RTMemFree(papPages);
930 if (pv)
931 {
932 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
933 if (pMemLnx)
934 {
935 pMemLnx->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
936 pMemLnx->cPages = 1;
937 pMemLnx->apPages[0] = pDummyPage;
938 *ppMem = &pMemLnx->Core;
939 return VINF_SUCCESS;
940 }
941 vunmap(pv);
942 }
943 }
944 __free_page(pDummyPage);
945 return VERR_NO_MEMORY;
946
947#else /* < 2.4.22 */
948 /*
949 * Could probably use ioremap here, but the caller is in a better position than us
950 * to select some safe physical memory.
951 */
952 return VERR_NOT_SUPPORTED;
953#endif
954}
955
956
957/**
958 * Worker for rtR0MemObjNativeReserveUser and rtR0MemObjNativerMapUser that creates
959 * an empty user space mapping.
960 *
961 * The caller takes care of acquiring the mmap_sem of the task.
962 *
963 * @returns Pointer to the mapping.
964 * (void *)-1 on failure.
965 * @param R3PtrFixed (RTR3PTR)-1 if anywhere, otherwise a specific location.
966 * @param cb The size of the mapping.
967 * @param uAlignment The alignment of the mapping.
968 * @param pTask The Linux task to create this mapping in.
969 * @param fProt The RTMEM_PROT_* mask.
970 */
971static void *rtR0MemObjLinuxDoMmap(RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, struct task_struct *pTask, unsigned fProt)
972{
973 unsigned fLnxProt;
974 unsigned long ulAddr;
975
976 /*
977 * Convert from IPRT protection to mman.h PROT_ and call do_mmap.
978 */
979 fProt &= (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
980 if (fProt == RTMEM_PROT_NONE)
981 fLnxProt = PROT_NONE;
982 else
983 {
984 fLnxProt = 0;
985 if (fProt & RTMEM_PROT_READ)
986 fLnxProt |= PROT_READ;
987 if (fProt & RTMEM_PROT_WRITE)
988 fLnxProt |= PROT_WRITE;
989 if (fProt & RTMEM_PROT_EXEC)
990 fLnxProt |= PROT_EXEC;
991 }
992
993 if (R3PtrFixed != (RTR3PTR)-1)
994 ulAddr = do_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
995 else
996 {
997 ulAddr = do_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
998 if ( !(ulAddr & ~PAGE_MASK)
999 && (ulAddr & (uAlignment - 1)))
1000 {
1001 /** @todo implement uAlignment properly... We'll probably need to make some dummy mappings to fill
1002 * up alignment gaps. This is of course complicated by fragmentation (which we might have cause
1003 * ourselves) and further by there begin two mmap strategies (top / bottom). */
1004 /* For now, just ignore uAlignment requirements... */
1005 }
1006 }
1007 if (ulAddr & ~PAGE_MASK) /* ~PAGE_MASK == PAGE_OFFSET_MASK */
1008 return (void *)-1;
1009 return (void *)ulAddr;
1010}
1011
1012
1013int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
1014{
1015 PRTR0MEMOBJLNX pMemLnx;
1016 void *pv;
1017 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1018 if (!pTask)
1019 return VERR_NOT_SUPPORTED;
1020
1021 /*
1022 * Check that the specified alignment is supported.
1023 */
1024 if (uAlignment > PAGE_SIZE)
1025 return VERR_NOT_SUPPORTED;
1026
1027 /*
1028 * Let rtR0MemObjLinuxDoMmap do the difficult bits.
1029 */
1030 down_write(&pTask->mm->mmap_sem);
1031 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cb, uAlignment, pTask, RTMEM_PROT_NONE);
1032 up_write(&pTask->mm->mmap_sem);
1033 if (pv == (void *)-1)
1034 return VERR_NO_MEMORY;
1035
1036 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
1037 if (!pMemLnx)
1038 {
1039 down_write(&pTask->mm->mmap_sem);
1040 MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, cb);
1041 up_write(&pTask->mm->mmap_sem);
1042 return VERR_NO_MEMORY;
1043 }
1044
1045 pMemLnx->Core.u.ResVirt.R0Process = R0Process;
1046 *ppMem = &pMemLnx->Core;
1047 return VINF_SUCCESS;
1048}
1049
1050
1051int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
1052 unsigned fProt, size_t offSub, size_t cbSub)
1053{
1054 int rc = VERR_NO_MEMORY;
1055 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1056 PRTR0MEMOBJLNX pMemLnx;
1057
1058 /* Fail if requested to do something we can't. */
1059 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
1060 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
1061 if (uAlignment > PAGE_SIZE)
1062 return VERR_NOT_SUPPORTED;
1063
1064 /*
1065 * Create the IPRT memory object.
1066 */
1067 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1068 if (pMemLnx)
1069 {
1070 if (pMemLnxToMap->cPages)
1071 {
1072#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1073 /*
1074 * Use vmap - 2.4.22 and later.
1075 */
1076 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, true /* kernel */);
1077# ifdef VM_MAP
1078 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_MAP, fPg);
1079# else
1080 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_ALLOC, fPg);
1081# endif
1082 if (pMemLnx->Core.pv)
1083 {
1084 pMemLnx->fMappedToRing0 = true;
1085 rc = VINF_SUCCESS;
1086 }
1087 else
1088 rc = VERR_MAP_FAILED;
1089
1090#else /* < 2.4.22 */
1091 /*
1092 * Only option here is to share mappings if possible and forget about fProt.
1093 */
1094 if (rtR0MemObjIsRing3(pMemToMap))
1095 rc = VERR_NOT_SUPPORTED;
1096 else
1097 {
1098 rc = VINF_SUCCESS;
1099 if (!pMemLnxToMap->Core.pv)
1100 rc = rtR0MemObjLinuxVMap(pMemLnxToMap, !!(fProt & RTMEM_PROT_EXEC));
1101 if (RT_SUCCESS(rc))
1102 {
1103 Assert(pMemLnxToMap->Core.pv);
1104 pMemLnx->Core.pv = pMemLnxToMap->Core.pv;
1105 }
1106 }
1107#endif
1108 }
1109 else
1110 {
1111 /*
1112 * MMIO / physical memory.
1113 */
1114 Assert(pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS && !pMemLnxToMap->Core.u.Phys.fAllocated);
1115 pMemLnx->Core.pv = ioremap(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb);
1116 if (pMemLnx->Core.pv)
1117 {
1118 /** @todo fix protection. */
1119 rc = VINF_SUCCESS;
1120 }
1121 }
1122 if (RT_SUCCESS(rc))
1123 {
1124 pMemLnx->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
1125 *ppMem = &pMemLnx->Core;
1126 return VINF_SUCCESS;
1127 }
1128 rtR0MemObjDelete(&pMemLnx->Core);
1129 }
1130
1131 return rc;
1132}
1133
1134
1135int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
1136{
1137 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1138 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1139 int rc = VERR_NO_MEMORY;
1140 PRTR0MEMOBJLNX pMemLnx;
1141
1142 /*
1143 * Check for restrictions.
1144 */
1145 if (!pTask)
1146 return VERR_NOT_SUPPORTED;
1147 if (uAlignment > PAGE_SIZE)
1148 return VERR_NOT_SUPPORTED;
1149
1150 /*
1151 * Create the IPRT memory object.
1152 */
1153 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1154 if (pMemLnx)
1155 {
1156 /*
1157 * Allocate user space mapping.
1158 */
1159 void *pv;
1160 down_write(&pTask->mm->mmap_sem);
1161 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, pMemLnxToMap->Core.cb, uAlignment, pTask, fProt);
1162 if (pv != (void *)-1)
1163 {
1164 /*
1165 * Map page by page into the mmap area.
1166 * This is generic, paranoid and not very efficient.
1167 */
1168 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, false /* user */);
1169 unsigned long ulAddrCur = (unsigned long)pv;
1170 const size_t cPages = pMemLnxToMap->Core.cb >> PAGE_SHIFT;
1171 size_t iPage;
1172 rc = 0;
1173 if (pMemLnxToMap->cPages)
1174 {
1175 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
1176 {
1177#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
1178 uint64_t u64Phys = page_to_phys(pMemLnxToMap->apPages[iPage]);
1179#endif
1180#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1181 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1182 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1183#endif
1184#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11) && defined(RT_ARCH_X86)
1185 /* remap_page_range() limitation on x86 */
1186 AssertBreakStmt(u64Phys < _4G, rc = VERR_NO_MEMORY);
1187#endif
1188
1189#if defined(VBOX_USE_INSERT_PAGE) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
1190 rc = vm_insert_page(vma, ulAddrCur, pMemLnxToMap->apPages[iPage]);
1191 vma->vm_flags |= VM_RESERVED; /* This flag helps making 100% sure some bad stuff wont happen (swap, core, ++). */
1192#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1193 rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1194#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1195 rc = remap_page_range(vma, ulAddrCur, u64Phys, PAGE_SIZE, fPg);
1196#else /* 2.4 */
1197 rc = remap_page_range(ulAddrCur, u64Phys, PAGE_SIZE, fPg);
1198#endif
1199 if (rc)
1200 break;
1201 }
1202 }
1203 else
1204 {
1205 RTHCPHYS Phys;
1206 if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS)
1207 Phys = pMemLnxToMap->Core.u.Phys.PhysBase;
1208 else if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_CONT)
1209 Phys = pMemLnxToMap->Core.u.Cont.Phys;
1210 else
1211 {
1212 AssertMsgFailed(("%d\n", pMemLnxToMap->Core.enmType));
1213 Phys = NIL_RTHCPHYS;
1214 }
1215 if (Phys != NIL_RTHCPHYS)
1216 {
1217 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE, Phys += PAGE_SIZE)
1218 {
1219#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1220 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1221 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1222#endif
1223#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11) && defined(RT_ARCH_X86)
1224 /* remap_page_range() limitation on x86 */
1225 AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
1226#endif
1227
1228#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1229 rc = remap_pfn_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1230#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1231 rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1232#else /* 2.4 */
1233 rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
1234#endif
1235 if (rc)
1236 break;
1237 }
1238 }
1239 }
1240 if (!rc)
1241 {
1242 up_write(&pTask->mm->mmap_sem);
1243
1244 pMemLnx->Core.pv = pv;
1245 pMemLnx->Core.u.Mapping.R0Process = R0Process;
1246 *ppMem = &pMemLnx->Core;
1247 return VINF_SUCCESS;
1248 }
1249
1250 /*
1251 * Bail out.
1252 */
1253 MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, pMemLnxToMap->Core.cb);
1254 if (rc != VERR_INTERNAL_ERROR)
1255 rc = VERR_NO_MEMORY;
1256 }
1257
1258 up_write(&pTask->mm->mmap_sem);
1259
1260 rtR0MemObjDelete(&pMemLnx->Core);
1261 }
1262
1263 return rc;
1264}
1265
1266
1267int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
1268{
1269 NOREF(pMem);
1270 NOREF(offSub);
1271 NOREF(cbSub);
1272 NOREF(fProt);
1273 return VERR_NOT_SUPPORTED;
1274}
1275
1276
1277RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
1278{
1279 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
1280
1281 if (pMemLnx->cPages)
1282 return page_to_phys(pMemLnx->apPages[iPage]);
1283
1284 switch (pMemLnx->Core.enmType)
1285 {
1286 case RTR0MEMOBJTYPE_CONT:
1287 return pMemLnx->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
1288
1289 case RTR0MEMOBJTYPE_PHYS:
1290 return pMemLnx->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
1291
1292 /* the parent knows */
1293 case RTR0MEMOBJTYPE_MAPPING:
1294 return rtR0MemObjNativeGetPagePhysAddr(pMemLnx->Core.uRel.Child.pParent, iPage);
1295
1296 /* cPages > 0 */
1297 case RTR0MEMOBJTYPE_LOW:
1298 case RTR0MEMOBJTYPE_LOCK:
1299 case RTR0MEMOBJTYPE_PHYS_NC:
1300 case RTR0MEMOBJTYPE_PAGE:
1301 default:
1302 AssertMsgFailed(("%d\n", pMemLnx->Core.enmType));
1303 /* fall thru */
1304
1305 case RTR0MEMOBJTYPE_RES_VIRT:
1306 return NIL_RTHCPHYS;
1307 }
1308}
1309
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