VirtualBox

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

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

set VM_LOCKED to pin pages allocated with mmap()

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