VirtualBox

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

Last change on this file since 21337 was 21314, checked in by vboxsync, 16 years ago

IPRT: Implemented rtR0MemObjNativeLockKernel for linux.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev
File size: 40.8 KB
Line 
1/* $Revision: 21314 $ */
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 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, GFP_USER, false /* non-contiguous */);
534#endif
535 if (RT_SUCCESS(rc))
536 {
537 rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
538 if (RT_SUCCESS(rc))
539 {
540 *ppMem = &pMemLnx->Core;
541 return rc;
542 }
543
544 rtR0MemObjLinuxFreePages(pMemLnx);
545 rtR0MemObjDelete(&pMemLnx->Core);
546 }
547
548 return rc;
549}
550
551
552int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
553{
554 PRTR0MEMOBJLNX pMemLnx;
555 int rc;
556
557#ifdef RT_ARCH_AMD64
558# ifdef GFP_DMA32
559 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, GFP_DMA32, true /* contiguous */);
560 if (RT_FAILURE(rc))
561# endif
562 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, GFP_DMA, true /* contiguous */);
563#else
564 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, GFP_USER, true /* contiguous */);
565#endif
566 if (RT_SUCCESS(rc))
567 {
568 rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
569 if (RT_SUCCESS(rc))
570 {
571#if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(CONFIG_HIGHMEM64G))
572 size_t iPage = pMemLnx->cPages;
573 while (iPage-- > 0)
574 Assert(page_to_phys(pMemLnx->apPages[iPage]) < _4G);
575#endif
576 pMemLnx->Core.u.Cont.Phys = page_to_phys(pMemLnx->apPages[0]);
577 *ppMem = &pMemLnx->Core;
578 return rc;
579 }
580
581 rtR0MemObjLinuxFreePages(pMemLnx);
582 rtR0MemObjDelete(&pMemLnx->Core);
583 }
584
585 return rc;
586}
587
588
589/**
590 * Worker for rtR0MemObjLinuxAllocPhysSub that tries one allocation strategy.
591 *
592 * @returns IPRT status.
593 * @param ppMemLnx Where to
594 * @param enmType The object type.
595 * @param cb The size of the allocation.
596 * @param PhysHighest See rtR0MemObjNativeAllocPhys.
597 * @param fGfp The Linux GFP flags to use for the allocation.
598 */
599static int rtR0MemObjLinuxAllocPhysSub2(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType, size_t cb, RTHCPHYS PhysHighest, unsigned fGfp)
600{
601 PRTR0MEMOBJLNX pMemLnx;
602 int rc;
603
604 rc = rtR0MemObjLinuxAllocPages(&pMemLnx, enmType, cb, fGfp,
605 enmType == RTR0MEMOBJTYPE_PHYS /* contiguous / non-contiguous */);
606 if (RT_FAILURE(rc))
607 return rc;
608
609 /*
610 * Check the addresses if necessary. (Can be optimized a bit for PHYS.)
611 */
612 if (PhysHighest != NIL_RTHCPHYS)
613 {
614 size_t iPage = pMemLnx->cPages;
615 while (iPage-- > 0)
616 if (page_to_phys(pMemLnx->apPages[iPage]) >= PhysHighest)
617 {
618 rtR0MemObjLinuxFreePages(pMemLnx);
619 rtR0MemObjDelete(&pMemLnx->Core);
620 return VERR_NO_MEMORY;
621 }
622 }
623
624 /*
625 * Complete the object.
626 */
627 if (enmType == RTR0MEMOBJTYPE_PHYS)
628 {
629 pMemLnx->Core.u.Phys.PhysBase = page_to_phys(pMemLnx->apPages[0]);
630 pMemLnx->Core.u.Phys.fAllocated = true;
631 }
632 *ppMem = &pMemLnx->Core;
633 return rc;
634}
635
636
637/**
638 * Worker for rtR0MemObjNativeAllocPhys and rtR0MemObjNativeAllocPhysNC.
639 *
640 * @returns IPRT status.
641 * @param ppMem Where to store the memory object pointer on success.
642 * @param enmType The object type.
643 * @param cb The size of the allocation.
644 * @param PhysHighest See rtR0MemObjNativeAllocPhys.
645 */
646static int rtR0MemObjLinuxAllocPhysSub(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType, size_t cb, RTHCPHYS PhysHighest)
647{
648 int rc;
649
650 /*
651 * There are two clear cases and that's the <=16MB and anything-goes ones.
652 * When the physical address limit is somewhere inbetween those two we'll
653 * just have to try, starting with HIGHUSER and working our way thru the
654 * different types, hoping we'll get lucky.
655 *
656 * We should probably move this physical address restriction logic up to
657 * the page alloc function as it would be more efficient there. But since
658 * we don't expect this to be a performance issue just yet it can wait.
659 */
660 if (PhysHighest == NIL_RTHCPHYS)
661 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_HIGHUSER);
662 else if (PhysHighest <= _1M * 16)
663 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_DMA);
664 else
665 {
666 rc = VERR_NO_MEMORY;
667 if (RT_FAILURE(rc))
668 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_HIGHUSER);
669 if (RT_FAILURE(rc))
670 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_USER);
671#ifdef GFP_DMA32
672 if (RT_FAILURE(rc))
673 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_DMA32);
674#endif
675 if (RT_FAILURE(rc))
676 rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_DMA);
677 }
678 return rc;
679}
680
681
682int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
683{
684 return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS, cb, PhysHighest);
685}
686
687
688int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
689{
690 return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS_NC, cb, PhysHighest);
691}
692
693
694int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
695{
696 /*
697 * All we need to do here is to validate that we can use
698 * ioremap on the specified address (32/64-bit dma_addr_t).
699 */
700 PRTR0MEMOBJLNX pMemLnx;
701 dma_addr_t PhysAddr = Phys;
702 AssertMsgReturn(PhysAddr == Phys, ("%#llx\n", (unsigned long long)Phys), VERR_ADDRESS_TOO_BIG);
703
704 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_PHYS, NULL, cb);
705 if (!pMemLnx)
706 return VERR_NO_MEMORY;
707
708 pMemLnx->Core.u.Phys.PhysBase = PhysAddr;
709 pMemLnx->Core.u.Phys.fAllocated = false;
710 Assert(!pMemLnx->cPages);
711 *ppMem = &pMemLnx->Core;
712 return VINF_SUCCESS;
713}
714
715
716int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
717{
718 const int cPages = cb >> PAGE_SHIFT;
719 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
720 struct vm_area_struct **papVMAs;
721 PRTR0MEMOBJLNX pMemLnx;
722 int rc = VERR_NO_MEMORY;
723
724 /*
725 * Check for valid task and size overflows.
726 */
727 if (!pTask)
728 return VERR_NOT_SUPPORTED;
729 if (((size_t)cPages << PAGE_SHIFT) != cb)
730 return VERR_OUT_OF_RANGE;
731
732 /*
733 * Allocate the memory object and a temporary buffer for the VMAs.
734 */
735 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
736 if (!pMemLnx)
737 return VERR_NO_MEMORY;
738
739 papVMAs = (struct vm_area_struct **)RTMemAlloc(sizeof(*papVMAs) * cPages);
740 if (papVMAs)
741 {
742 down_read(&pTask->mm->mmap_sem);
743
744 /*
745 * Get user pages.
746 */
747 rc = get_user_pages(pTask, /* Task for fault acounting. */
748 pTask->mm, /* Whose pages. */
749 R3Ptr, /* Where from. */
750 cPages, /* How many pages. */
751 1, /* Write to memory. */
752 0, /* force. */
753 &pMemLnx->apPages[0], /* Page array. */
754 papVMAs); /* vmas */
755 if (rc == cPages)
756 {
757 /*
758 * Flush dcache (required?), protect against fork and _really_ pin the page
759 * table entries. get_user_pages() will protect against swapping out the
760 * pages but it will NOT protect against removing page table entries. This
761 * can be achieved with
762 * - using mlock / mmap(..., MAP_LOCKED, ...) from userland. This requires
763 * an appropriate limit set up with setrlimit(..., RLIMIT_MEMLOCK, ...).
764 * Usual Linux distributions support only a limited size of locked pages
765 * (e.g. 32KB).
766 * - setting the PageReserved bit (as we do in rtR0MemObjLinuxAllocPages()
767 * or by
768 * - setting the VM_LOCKED flag. This is the same as doing mlock() without
769 * a range check.
770 */
771 /** @todo The Linux fork() protection will require more work if this API
772 * is to be used for anything but locking VM pages. */
773 while (rc-- > 0)
774 {
775 flush_dcache_page(pMemLnx->apPages[rc]);
776 papVMAs[rc]->vm_flags |= (VM_DONTCOPY | VM_LOCKED);
777 }
778
779 up_read(&pTask->mm->mmap_sem);
780
781 RTMemFree(papVMAs);
782
783 pMemLnx->Core.u.Lock.R0Process = R0Process;
784 pMemLnx->cPages = cPages;
785 Assert(!pMemLnx->fMappedToRing0);
786 *ppMem = &pMemLnx->Core;
787
788 return VINF_SUCCESS;
789 }
790
791 /*
792 * Failed - we need to unlock any pages that we succeeded to lock.
793 */
794 while (rc-- > 0)
795 {
796 if (!PageReserved(pMemLnx->apPages[rc]))
797 SetPageDirty(pMemLnx->apPages[rc]);
798 page_cache_release(pMemLnx->apPages[rc]);
799 }
800
801 up_read(&pTask->mm->mmap_sem);
802
803 RTMemFree(papVMAs);
804 rc = VERR_LOCK_FAILED;
805 }
806
807 rtR0MemObjDelete(&pMemLnx->Core);
808 return rc;
809}
810
811
812int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
813{
814 void *pvLast = (uint8_t *)pv + cb - 1;
815 size_t const cPages = cb >> PAGE_SHIFT;
816 PRTR0MEMOBJLNX pMemLnx;
817 bool fLinearMapping;
818 int rc;
819 uint8_t *pbPage;
820 size_t iPage;
821
822 /*
823 * Classify the memory and check that we can deal with it.
824 */
825#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
826 fLinearMapping = virt_addr_valid(pvLast) && virt_addr_valid(pv);
827#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0)
828 fLinearMapping = VALID_PAGE(virt_to_page(pvLast)) && VALID_PAGE(virt_to_page(pv));
829#else
830# error "not supported"
831#endif
832 if (!fLinearMapping)
833 {
834#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 19)
835 if ( !RTR0MemKernelIsValidAddr(pv)
836 || !RTR0MemKernelIsValidAddr(pv + cb))
837#endif
838 return VERR_INVALID_PARAMETER;
839 }
840
841 /*
842 * Allocate the memory object.
843 */
844 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, pv, cb);
845 if (!pMemLnx)
846 return VERR_NO_MEMORY;
847
848 /*
849 * Gather the pages.
850 * We ASSUME all kernel pages are non-swappable.
851 */
852 rc = VINF_SUCCESS;
853 pbPage = (uint8_t *)pvLast;
854 iPage = cPages;
855#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 19)
856 if (!fLinearMapping)
857 {
858 while (iPage-- > 0)
859 {
860 struct page *pPage = vmalloc_to_page(pbPage);
861 if (RT_UNLIKELY(!pPage))
862 {
863 rc = VERR_LOCK_FAILED;
864 break;
865 }
866 pMemLnx->apPages[iPage] = pPage;
867 pbPage -= PAGE_SIZE;
868 }
869 }
870 else
871#endif
872 {
873 while (iPage-- > 0)
874 {
875 pMemLnx->apPages[iPage] = virt_to_page(pbPage);
876 pbPage -= PAGE_SIZE;
877 }
878 }
879 if (RT_SUCCESS(rc))
880 {
881 /*
882 * Complete the memory object and return.
883 */
884 pMemLnx->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
885 pMemLnx->cPages = cPages;
886 Assert(!pMemLnx->fMappedToRing0);
887 *ppMem = &pMemLnx->Core;
888
889 return VINF_SUCCESS;
890 }
891
892 rtR0MemObjDelete(&pMemLnx->Core);
893 return rc;
894}
895
896
897int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
898{
899#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
900 const size_t cPages = cb >> PAGE_SHIFT;
901 struct page *pDummyPage;
902 struct page **papPages;
903
904 /* check for unsupported stuff. */
905 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
906 AssertMsgReturn(uAlignment <= PAGE_SIZE, ("%#x\n", uAlignment), VERR_NOT_SUPPORTED);
907
908 /*
909 * Allocate a dummy page and create a page pointer array for vmap such that
910 * the dummy page is mapped all over the reserved area.
911 */
912 pDummyPage = alloc_page(GFP_HIGHUSER);
913 if (!pDummyPage)
914 return VERR_NO_MEMORY;
915 papPages = RTMemAlloc(sizeof(*papPages) * cPages);
916 if (papPages)
917 {
918 void *pv;
919 size_t iPage = cPages;
920 while (iPage-- > 0)
921 papPages[iPage] = pDummyPage;
922# ifdef VM_MAP
923 pv = vmap(papPages, cPages, VM_MAP, PAGE_KERNEL_RO);
924# else
925 pv = vmap(papPages, cPages, VM_ALLOC, PAGE_KERNEL_RO);
926# endif
927 RTMemFree(papPages);
928 if (pv)
929 {
930 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
931 if (pMemLnx)
932 {
933 pMemLnx->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
934 pMemLnx->cPages = 1;
935 pMemLnx->apPages[0] = pDummyPage;
936 *ppMem = &pMemLnx->Core;
937 return VINF_SUCCESS;
938 }
939 vunmap(pv);
940 }
941 }
942 __free_page(pDummyPage);
943 return VERR_NO_MEMORY;
944
945#else /* < 2.4.22 */
946 /*
947 * Could probably use ioremap here, but the caller is in a better position than us
948 * to select some safe physical memory.
949 */
950 return VERR_NOT_SUPPORTED;
951#endif
952}
953
954
955/**
956 * Worker for rtR0MemObjNativeReserveUser and rtR0MemObjNativerMapUser that creates
957 * an empty user space mapping.
958 *
959 * The caller takes care of acquiring the mmap_sem of the task.
960 *
961 * @returns Pointer to the mapping.
962 * (void *)-1 on failure.
963 * @param R3PtrFixed (RTR3PTR)-1 if anywhere, otherwise a specific location.
964 * @param cb The size of the mapping.
965 * @param uAlignment The alignment of the mapping.
966 * @param pTask The Linux task to create this mapping in.
967 * @param fProt The RTMEM_PROT_* mask.
968 */
969static void *rtR0MemObjLinuxDoMmap(RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, struct task_struct *pTask, unsigned fProt)
970{
971 unsigned fLnxProt;
972 unsigned long ulAddr;
973
974 /*
975 * Convert from IPRT protection to mman.h PROT_ and call do_mmap.
976 */
977 fProt &= (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
978 if (fProt == RTMEM_PROT_NONE)
979 fLnxProt = PROT_NONE;
980 else
981 {
982 fLnxProt = 0;
983 if (fProt & RTMEM_PROT_READ)
984 fLnxProt |= PROT_READ;
985 if (fProt & RTMEM_PROT_WRITE)
986 fLnxProt |= PROT_WRITE;
987 if (fProt & RTMEM_PROT_EXEC)
988 fLnxProt |= PROT_EXEC;
989 }
990
991 if (R3PtrFixed != (RTR3PTR)-1)
992 ulAddr = do_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
993 else
994 {
995 ulAddr = do_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
996 if ( !(ulAddr & ~PAGE_MASK)
997 && (ulAddr & (uAlignment - 1)))
998 {
999 /** @todo implement uAlignment properly... We'll probably need to make some dummy mappings to fill
1000 * up alignment gaps. This is of course complicated by fragmentation (which we might have cause
1001 * ourselves) and further by there begin two mmap strategies (top / bottom). */
1002 /* For now, just ignore uAlignment requirements... */
1003 }
1004 }
1005 if (ulAddr & ~PAGE_MASK) /* ~PAGE_MASK == PAGE_OFFSET_MASK */
1006 return (void *)-1;
1007 return (void *)ulAddr;
1008}
1009
1010
1011int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
1012{
1013 PRTR0MEMOBJLNX pMemLnx;
1014 void *pv;
1015 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1016 if (!pTask)
1017 return VERR_NOT_SUPPORTED;
1018
1019 /*
1020 * Let rtR0MemObjLinuxDoMmap do the difficult bits.
1021 */
1022 down_write(&pTask->mm->mmap_sem);
1023 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cb, uAlignment, pTask, RTMEM_PROT_NONE);
1024 up_write(&pTask->mm->mmap_sem);
1025 if (pv == (void *)-1)
1026 return VERR_NO_MEMORY;
1027
1028 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
1029 if (!pMemLnx)
1030 {
1031 down_write(&pTask->mm->mmap_sem);
1032 MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, cb);
1033 up_write(&pTask->mm->mmap_sem);
1034 return VERR_NO_MEMORY;
1035 }
1036
1037 pMemLnx->Core.u.ResVirt.R0Process = R0Process;
1038 *ppMem = &pMemLnx->Core;
1039 return VINF_SUCCESS;
1040}
1041
1042
1043int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
1044 unsigned fProt, size_t offSub, size_t cbSub)
1045{
1046 int rc = VERR_NO_MEMORY;
1047 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1048 PRTR0MEMOBJLNX pMemLnx;
1049
1050 /* Fail if requested to do something we can't. */
1051 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
1052 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
1053 AssertMsgReturn(uAlignment <= PAGE_SIZE, ("%#x\n", uAlignment), VERR_NOT_SUPPORTED);
1054
1055 /*
1056 * Create the IPRT memory object.
1057 */
1058 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1059 if (pMemLnx)
1060 {
1061 if (pMemLnxToMap->cPages)
1062 {
1063#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1064 /*
1065 * Use vmap - 2.4.22 and later.
1066 */
1067 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, true /* kernel */);
1068# ifdef VM_MAP
1069 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_MAP, fPg);
1070# else
1071 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_ALLOC, fPg);
1072# endif
1073 if (pMemLnx->Core.pv)
1074 {
1075 pMemLnx->fMappedToRing0 = true;
1076 rc = VINF_SUCCESS;
1077 }
1078 else
1079 rc = VERR_MAP_FAILED;
1080
1081#else /* < 2.4.22 */
1082 /*
1083 * Only option here is to share mappings if possible and forget about fProt.
1084 */
1085 if (rtR0MemObjIsRing3(pMemToMap))
1086 rc = VERR_NOT_SUPPORTED;
1087 else
1088 {
1089 rc = VINF_SUCCESS;
1090 if (!pMemLnxToMap->Core.pv)
1091 rc = rtR0MemObjLinuxVMap(pMemLnxToMap, !!(fProt & RTMEM_PROT_EXEC));
1092 if (RT_SUCCESS(rc))
1093 {
1094 Assert(pMemLnxToMap->Core.pv);
1095 pMemLnx->Core.pv = pMemLnxToMap->Core.pv;
1096 }
1097 }
1098#endif
1099 }
1100 else
1101 {
1102 /*
1103 * MMIO / physical memory.
1104 */
1105 Assert(pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS && !pMemLnxToMap->Core.u.Phys.fAllocated);
1106 pMemLnx->Core.pv = ioremap(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb);
1107 if (pMemLnx->Core.pv)
1108 {
1109 /** @todo fix protection. */
1110 rc = VINF_SUCCESS;
1111 }
1112 }
1113 if (RT_SUCCESS(rc))
1114 {
1115 pMemLnx->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
1116 *ppMem = &pMemLnx->Core;
1117 return VINF_SUCCESS;
1118 }
1119 rtR0MemObjDelete(&pMemLnx->Core);
1120 }
1121
1122 return rc;
1123}
1124
1125
1126int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
1127{
1128 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1129 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1130 int rc = VERR_NO_MEMORY;
1131 PRTR0MEMOBJLNX pMemLnx;
1132
1133 /*
1134 * Check for restrictions.
1135 */
1136 if (!pTask)
1137 return VERR_NOT_SUPPORTED;
1138
1139 /*
1140 * Create the IPRT memory object.
1141 */
1142 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1143 if (pMemLnx)
1144 {
1145 /*
1146 * Allocate user space mapping.
1147 */
1148 void *pv;
1149 down_write(&pTask->mm->mmap_sem);
1150 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, pMemLnxToMap->Core.cb, uAlignment, pTask, fProt);
1151 if (pv != (void *)-1)
1152 {
1153 /*
1154 * Map page by page into the mmap area.
1155 * This is generic, paranoid and not very efficient.
1156 */
1157 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, false /* user */);
1158 unsigned long ulAddrCur = (unsigned long)pv;
1159 const size_t cPages = pMemLnxToMap->Core.cb >> PAGE_SHIFT;
1160 size_t iPage;
1161 rc = 0;
1162 if (pMemLnxToMap->cPages)
1163 {
1164 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
1165 {
1166#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1167 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1168 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1169#endif
1170
1171#if defined(VBOX_USE_INSERT_PAGE) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
1172 rc = vm_insert_page(vma, ulAddrCur, pMemLnxToMap->apPages[iPage]);
1173 vma->vm_flags |= VM_RESERVED; /* This flag helps making 100% sure some bad stuff wont happen (swap, core, ++). */
1174#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1175 rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1176#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1177 rc = remap_page_range(vma, ulAddrCur, page_to_phys(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1178#else /* 2.4 */
1179 rc = remap_page_range(ulAddrCur, page_to_phys(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1180#endif
1181 if (rc)
1182 break;
1183 }
1184 }
1185 else
1186 {
1187 RTHCPHYS Phys;
1188 if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS)
1189 Phys = pMemLnxToMap->Core.u.Phys.PhysBase;
1190 else if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_CONT)
1191 Phys = pMemLnxToMap->Core.u.Cont.Phys;
1192 else
1193 {
1194 AssertMsgFailed(("%d\n", pMemLnxToMap->Core.enmType));
1195 Phys = NIL_RTHCPHYS;
1196 }
1197 if (Phys != NIL_RTHCPHYS)
1198 {
1199 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE, Phys += PAGE_SIZE)
1200 {
1201#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1202 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1203 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1204#endif
1205
1206#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1207 rc = remap_pfn_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1208#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1209 rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1210#else /* 2.4 */
1211 rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
1212#endif
1213 if (rc)
1214 break;
1215 }
1216 }
1217 }
1218 if (!rc)
1219 {
1220 up_write(&pTask->mm->mmap_sem);
1221
1222 pMemLnx->Core.pv = pv;
1223 pMemLnx->Core.u.Mapping.R0Process = R0Process;
1224 *ppMem = &pMemLnx->Core;
1225 return VINF_SUCCESS;
1226 }
1227
1228 /*
1229 * Bail out.
1230 */
1231 MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, pMemLnxToMap->Core.cb);
1232 if (rc != VERR_INTERNAL_ERROR)
1233 rc = VERR_NO_MEMORY;
1234 }
1235
1236 up_write(&pTask->mm->mmap_sem);
1237
1238 rtR0MemObjDelete(&pMemLnx->Core);
1239 }
1240
1241 return rc;
1242}
1243
1244
1245int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
1246{
1247 NOREF(pMem);
1248 NOREF(offSub);
1249 NOREF(cbSub);
1250 NOREF(fProt);
1251 return VERR_NOT_SUPPORTED;
1252}
1253
1254
1255RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
1256{
1257 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
1258
1259 if (pMemLnx->cPages)
1260 return page_to_phys(pMemLnx->apPages[iPage]);
1261
1262 switch (pMemLnx->Core.enmType)
1263 {
1264 case RTR0MEMOBJTYPE_CONT:
1265 return pMemLnx->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
1266
1267 case RTR0MEMOBJTYPE_PHYS:
1268 return pMemLnx->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
1269
1270 /* the parent knows */
1271 case RTR0MEMOBJTYPE_MAPPING:
1272 return rtR0MemObjNativeGetPagePhysAddr(pMemLnx->Core.uRel.Child.pParent, iPage);
1273
1274 /* cPages > 0 */
1275 case RTR0MEMOBJTYPE_LOW:
1276 case RTR0MEMOBJTYPE_LOCK:
1277 case RTR0MEMOBJTYPE_PHYS_NC:
1278 case RTR0MEMOBJTYPE_PAGE:
1279 default:
1280 AssertMsgFailed(("%d\n", pMemLnx->Core.enmType));
1281 /* fall thru */
1282
1283 case RTR0MEMOBJTYPE_RES_VIRT:
1284 return NIL_RTHCPHYS;
1285 }
1286}
1287
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