VirtualBox

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

Last change on this file since 23454 was 22680, checked in by vboxsync, 15 years ago

memobj-r0drv-linux.c: the goto police.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette