VirtualBox

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

Last change on this file since 33640 was 33640, checked in by vboxsync, 14 years ago

Linux host page allocator: don't use system reserves for >=1M reqs

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