VirtualBox

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

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

RTR0MemObj*: correctly reject unsupported alignment requirements (VERR_NOT_SUPPORTED).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev
File size: 40.9 KB
Line 
1/* $Revision: 21497 $ */
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 if (uAlignment > PAGE_SIZE)
907 return VERR_NOT_SUPPORTED;
908
909 /*
910 * Allocate a dummy page and create a page pointer array for vmap such that
911 * the dummy page is mapped all over the reserved area.
912 */
913 pDummyPage = alloc_page(GFP_HIGHUSER);
914 if (!pDummyPage)
915 return VERR_NO_MEMORY;
916 papPages = RTMemAlloc(sizeof(*papPages) * cPages);
917 if (papPages)
918 {
919 void *pv;
920 size_t iPage = cPages;
921 while (iPage-- > 0)
922 papPages[iPage] = pDummyPage;
923# ifdef VM_MAP
924 pv = vmap(papPages, cPages, VM_MAP, PAGE_KERNEL_RO);
925# else
926 pv = vmap(papPages, cPages, VM_ALLOC, PAGE_KERNEL_RO);
927# endif
928 RTMemFree(papPages);
929 if (pv)
930 {
931 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
932 if (pMemLnx)
933 {
934 pMemLnx->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
935 pMemLnx->cPages = 1;
936 pMemLnx->apPages[0] = pDummyPage;
937 *ppMem = &pMemLnx->Core;
938 return VINF_SUCCESS;
939 }
940 vunmap(pv);
941 }
942 }
943 __free_page(pDummyPage);
944 return VERR_NO_MEMORY;
945
946#else /* < 2.4.22 */
947 /*
948 * Could probably use ioremap here, but the caller is in a better position than us
949 * to select some safe physical memory.
950 */
951 return VERR_NOT_SUPPORTED;
952#endif
953}
954
955
956/**
957 * Worker for rtR0MemObjNativeReserveUser and rtR0MemObjNativerMapUser that creates
958 * an empty user space mapping.
959 *
960 * The caller takes care of acquiring the mmap_sem of the task.
961 *
962 * @returns Pointer to the mapping.
963 * (void *)-1 on failure.
964 * @param R3PtrFixed (RTR3PTR)-1 if anywhere, otherwise a specific location.
965 * @param cb The size of the mapping.
966 * @param uAlignment The alignment of the mapping.
967 * @param pTask The Linux task to create this mapping in.
968 * @param fProt The RTMEM_PROT_* mask.
969 */
970static void *rtR0MemObjLinuxDoMmap(RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, struct task_struct *pTask, unsigned fProt)
971{
972 unsigned fLnxProt;
973 unsigned long ulAddr;
974
975 /*
976 * Convert from IPRT protection to mman.h PROT_ and call do_mmap.
977 */
978 fProt &= (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
979 if (fProt == RTMEM_PROT_NONE)
980 fLnxProt = PROT_NONE;
981 else
982 {
983 fLnxProt = 0;
984 if (fProt & RTMEM_PROT_READ)
985 fLnxProt |= PROT_READ;
986 if (fProt & RTMEM_PROT_WRITE)
987 fLnxProt |= PROT_WRITE;
988 if (fProt & RTMEM_PROT_EXEC)
989 fLnxProt |= PROT_EXEC;
990 }
991
992 if (R3PtrFixed != (RTR3PTR)-1)
993 ulAddr = do_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
994 else
995 {
996 ulAddr = do_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
997 if ( !(ulAddr & ~PAGE_MASK)
998 && (ulAddr & (uAlignment - 1)))
999 {
1000 /** @todo implement uAlignment properly... We'll probably need to make some dummy mappings to fill
1001 * up alignment gaps. This is of course complicated by fragmentation (which we might have cause
1002 * ourselves) and further by there begin two mmap strategies (top / bottom). */
1003 /* For now, just ignore uAlignment requirements... */
1004 }
1005 }
1006 if (ulAddr & ~PAGE_MASK) /* ~PAGE_MASK == PAGE_OFFSET_MASK */
1007 return (void *)-1;
1008 return (void *)ulAddr;
1009}
1010
1011
1012int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
1013{
1014 PRTR0MEMOBJLNX pMemLnx;
1015 void *pv;
1016 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1017 if (!pTask)
1018 return VERR_NOT_SUPPORTED;
1019
1020 /*
1021 * Check that the specified alignment is supported.
1022 */
1023 if (uAlignment > PAGE_SIZE)
1024 return VERR_NOT_SUPPORTED;
1025
1026 /*
1027 * Let rtR0MemObjLinuxDoMmap do the difficult bits.
1028 */
1029 down_write(&pTask->mm->mmap_sem);
1030 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cb, uAlignment, pTask, RTMEM_PROT_NONE);
1031 up_write(&pTask->mm->mmap_sem);
1032 if (pv == (void *)-1)
1033 return VERR_NO_MEMORY;
1034
1035 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
1036 if (!pMemLnx)
1037 {
1038 down_write(&pTask->mm->mmap_sem);
1039 MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, cb);
1040 up_write(&pTask->mm->mmap_sem);
1041 return VERR_NO_MEMORY;
1042 }
1043
1044 pMemLnx->Core.u.ResVirt.R0Process = R0Process;
1045 *ppMem = &pMemLnx->Core;
1046 return VINF_SUCCESS;
1047}
1048
1049
1050int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
1051 unsigned fProt, size_t offSub, size_t cbSub)
1052{
1053 int rc = VERR_NO_MEMORY;
1054 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1055 PRTR0MEMOBJLNX pMemLnx;
1056
1057 /* Fail if requested to do something we can't. */
1058 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
1059 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
1060 if (uAlignment > PAGE_SIZE)
1061 return VERR_NOT_SUPPORTED;
1062
1063 /*
1064 * Create the IPRT memory object.
1065 */
1066 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1067 if (pMemLnx)
1068 {
1069 if (pMemLnxToMap->cPages)
1070 {
1071#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1072 /*
1073 * Use vmap - 2.4.22 and later.
1074 */
1075 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, true /* kernel */);
1076# ifdef VM_MAP
1077 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_MAP, fPg);
1078# else
1079 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_ALLOC, fPg);
1080# endif
1081 if (pMemLnx->Core.pv)
1082 {
1083 pMemLnx->fMappedToRing0 = true;
1084 rc = VINF_SUCCESS;
1085 }
1086 else
1087 rc = VERR_MAP_FAILED;
1088
1089#else /* < 2.4.22 */
1090 /*
1091 * Only option here is to share mappings if possible and forget about fProt.
1092 */
1093 if (rtR0MemObjIsRing3(pMemToMap))
1094 rc = VERR_NOT_SUPPORTED;
1095 else
1096 {
1097 rc = VINF_SUCCESS;
1098 if (!pMemLnxToMap->Core.pv)
1099 rc = rtR0MemObjLinuxVMap(pMemLnxToMap, !!(fProt & RTMEM_PROT_EXEC));
1100 if (RT_SUCCESS(rc))
1101 {
1102 Assert(pMemLnxToMap->Core.pv);
1103 pMemLnx->Core.pv = pMemLnxToMap->Core.pv;
1104 }
1105 }
1106#endif
1107 }
1108 else
1109 {
1110 /*
1111 * MMIO / physical memory.
1112 */
1113 Assert(pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS && !pMemLnxToMap->Core.u.Phys.fAllocated);
1114 pMemLnx->Core.pv = ioremap(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb);
1115 if (pMemLnx->Core.pv)
1116 {
1117 /** @todo fix protection. */
1118 rc = VINF_SUCCESS;
1119 }
1120 }
1121 if (RT_SUCCESS(rc))
1122 {
1123 pMemLnx->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
1124 *ppMem = &pMemLnx->Core;
1125 return VINF_SUCCESS;
1126 }
1127 rtR0MemObjDelete(&pMemLnx->Core);
1128 }
1129
1130 return rc;
1131}
1132
1133
1134int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
1135{
1136 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1137 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1138 int rc = VERR_NO_MEMORY;
1139 PRTR0MEMOBJLNX pMemLnx;
1140
1141 /*
1142 * Check for restrictions.
1143 */
1144 if (!pTask)
1145 return VERR_NOT_SUPPORTED;
1146 if (uAlignment > PAGE_SIZE)
1147 return VERR_NOT_SUPPORTED;
1148
1149 /*
1150 * Create the IPRT memory object.
1151 */
1152 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1153 if (pMemLnx)
1154 {
1155 /*
1156 * Allocate user space mapping.
1157 */
1158 void *pv;
1159 down_write(&pTask->mm->mmap_sem);
1160 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, pMemLnxToMap->Core.cb, uAlignment, pTask, fProt);
1161 if (pv != (void *)-1)
1162 {
1163 /*
1164 * Map page by page into the mmap area.
1165 * This is generic, paranoid and not very efficient.
1166 */
1167 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, false /* user */);
1168 unsigned long ulAddrCur = (unsigned long)pv;
1169 const size_t cPages = pMemLnxToMap->Core.cb >> PAGE_SHIFT;
1170 size_t iPage;
1171 rc = 0;
1172 if (pMemLnxToMap->cPages)
1173 {
1174 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
1175 {
1176#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1177 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1178 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1179#endif
1180
1181#if defined(VBOX_USE_INSERT_PAGE) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
1182 rc = vm_insert_page(vma, ulAddrCur, pMemLnxToMap->apPages[iPage]);
1183 vma->vm_flags |= VM_RESERVED; /* This flag helps making 100% sure some bad stuff wont happen (swap, core, ++). */
1184#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1185 rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1186#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1187 rc = remap_page_range(vma, ulAddrCur, page_to_phys(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1188#else /* 2.4 */
1189 rc = remap_page_range(ulAddrCur, page_to_phys(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1190#endif
1191 if (rc)
1192 break;
1193 }
1194 }
1195 else
1196 {
1197 RTHCPHYS Phys;
1198 if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS)
1199 Phys = pMemLnxToMap->Core.u.Phys.PhysBase;
1200 else if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_CONT)
1201 Phys = pMemLnxToMap->Core.u.Cont.Phys;
1202 else
1203 {
1204 AssertMsgFailed(("%d\n", pMemLnxToMap->Core.enmType));
1205 Phys = NIL_RTHCPHYS;
1206 }
1207 if (Phys != NIL_RTHCPHYS)
1208 {
1209 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE, Phys += PAGE_SIZE)
1210 {
1211#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1212 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1213 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1214#endif
1215
1216#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1217 rc = remap_pfn_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1218#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1219 rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1220#else /* 2.4 */
1221 rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
1222#endif
1223 if (rc)
1224 break;
1225 }
1226 }
1227 }
1228 if (!rc)
1229 {
1230 up_write(&pTask->mm->mmap_sem);
1231
1232 pMemLnx->Core.pv = pv;
1233 pMemLnx->Core.u.Mapping.R0Process = R0Process;
1234 *ppMem = &pMemLnx->Core;
1235 return VINF_SUCCESS;
1236 }
1237
1238 /*
1239 * Bail out.
1240 */
1241 MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, pMemLnxToMap->Core.cb);
1242 if (rc != VERR_INTERNAL_ERROR)
1243 rc = VERR_NO_MEMORY;
1244 }
1245
1246 up_write(&pTask->mm->mmap_sem);
1247
1248 rtR0MemObjDelete(&pMemLnx->Core);
1249 }
1250
1251 return rc;
1252}
1253
1254
1255int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
1256{
1257 NOREF(pMem);
1258 NOREF(offSub);
1259 NOREF(cbSub);
1260 NOREF(fProt);
1261 return VERR_NOT_SUPPORTED;
1262}
1263
1264
1265RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
1266{
1267 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
1268
1269 if (pMemLnx->cPages)
1270 return page_to_phys(pMemLnx->apPages[iPage]);
1271
1272 switch (pMemLnx->Core.enmType)
1273 {
1274 case RTR0MEMOBJTYPE_CONT:
1275 return pMemLnx->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
1276
1277 case RTR0MEMOBJTYPE_PHYS:
1278 return pMemLnx->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
1279
1280 /* the parent knows */
1281 case RTR0MEMOBJTYPE_MAPPING:
1282 return rtR0MemObjNativeGetPagePhysAddr(pMemLnx->Core.uRel.Child.pParent, iPage);
1283
1284 /* cPages > 0 */
1285 case RTR0MEMOBJTYPE_LOW:
1286 case RTR0MEMOBJTYPE_LOCK:
1287 case RTR0MEMOBJTYPE_PHYS_NC:
1288 case RTR0MEMOBJTYPE_PAGE:
1289 default:
1290 AssertMsgFailed(("%d\n", pMemLnx->Core.enmType));
1291 /* fall thru */
1292
1293 case RTR0MEMOBJTYPE_RES_VIRT:
1294 return NIL_RTHCPHYS;
1295 }
1296}
1297
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