VirtualBox

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

Last change on this file since 25645 was 23611, checked in by vboxsync, 15 years ago

typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev
File size: 44.8 KB
Line 
1/* $Revision: 23611 $ */
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, uint32_t fAccess, 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 NOREF(fAccess);
744
745 /*
746 * Check for valid task and size overflows.
747 */
748 if (!pTask)
749 return VERR_NOT_SUPPORTED;
750 if (((size_t)cPages << PAGE_SHIFT) != cb)
751 return VERR_OUT_OF_RANGE;
752
753 /*
754 * Allocate the memory object and a temporary buffer for the VMAs.
755 */
756 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
757 if (!pMemLnx)
758 return VERR_NO_MEMORY;
759
760 papVMAs = (struct vm_area_struct **)RTMemAlloc(sizeof(*papVMAs) * cPages);
761 if (papVMAs)
762 {
763 down_read(&pTask->mm->mmap_sem);
764
765 /*
766 * Get user pages.
767 */
768 rc = get_user_pages(pTask, /* Task for fault acounting. */
769 pTask->mm, /* Whose pages. */
770 R3Ptr, /* Where from. */
771 cPages, /* How many pages. */
772 1, /* Write to memory. */
773 0, /* force. */
774 &pMemLnx->apPages[0], /* Page array. */
775 papVMAs); /* vmas */
776 if (rc == cPages)
777 {
778 /*
779 * Flush dcache (required?), protect against fork and _really_ pin the page
780 * table entries. get_user_pages() will protect against swapping out the
781 * pages but it will NOT protect against removing page table entries. This
782 * can be achieved with
783 * - using mlock / mmap(..., MAP_LOCKED, ...) from userland. This requires
784 * an appropriate limit set up with setrlimit(..., RLIMIT_MEMLOCK, ...).
785 * Usual Linux distributions support only a limited size of locked pages
786 * (e.g. 32KB).
787 * - setting the PageReserved bit (as we do in rtR0MemObjLinuxAllocPages()
788 * or by
789 * - setting the VM_LOCKED flag. This is the same as doing mlock() without
790 * a range check.
791 */
792 /** @todo The Linux fork() protection will require more work if this API
793 * is to be used for anything but locking VM pages. */
794 while (rc-- > 0)
795 {
796 flush_dcache_page(pMemLnx->apPages[rc]);
797 papVMAs[rc]->vm_flags |= (VM_DONTCOPY | VM_LOCKED);
798 }
799
800 up_read(&pTask->mm->mmap_sem);
801
802 RTMemFree(papVMAs);
803
804 pMemLnx->Core.u.Lock.R0Process = R0Process;
805 pMemLnx->cPages = cPages;
806 Assert(!pMemLnx->fMappedToRing0);
807 *ppMem = &pMemLnx->Core;
808
809 return VINF_SUCCESS;
810 }
811
812 /*
813 * Failed - we need to unlock any pages that we succeeded to lock.
814 */
815 while (rc-- > 0)
816 {
817 if (!PageReserved(pMemLnx->apPages[rc]))
818 SetPageDirty(pMemLnx->apPages[rc]);
819 page_cache_release(pMemLnx->apPages[rc]);
820 }
821
822 up_read(&pTask->mm->mmap_sem);
823
824 RTMemFree(papVMAs);
825 rc = VERR_LOCK_FAILED;
826 }
827
828 rtR0MemObjDelete(&pMemLnx->Core);
829 return rc;
830}
831
832
833int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
834{
835 void *pvLast = (uint8_t *)pv + cb - 1;
836 size_t const cPages = cb >> PAGE_SHIFT;
837 PRTR0MEMOBJLNX pMemLnx;
838 bool fLinearMapping;
839 int rc;
840 uint8_t *pbPage;
841 size_t iPage;
842 NOREF(fAccess);
843
844 /*
845 * Classify the memory and check that we can deal with it.
846 */
847#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
848 fLinearMapping = virt_addr_valid(pvLast) && virt_addr_valid(pv);
849#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0)
850 fLinearMapping = VALID_PAGE(virt_to_page(pvLast)) && VALID_PAGE(virt_to_page(pv));
851#else
852# error "not supported"
853#endif
854 if (!fLinearMapping)
855 {
856#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 19)
857 if ( !RTR0MemKernelIsValidAddr(pv)
858 || !RTR0MemKernelIsValidAddr(pv + cb))
859#endif
860 return VERR_INVALID_PARAMETER;
861 }
862
863 /*
864 * Allocate the memory object.
865 */
866 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, pv, cb);
867 if (!pMemLnx)
868 return VERR_NO_MEMORY;
869
870 /*
871 * Gather the pages.
872 * We ASSUME all kernel pages are non-swappable.
873 */
874 rc = VINF_SUCCESS;
875 pbPage = (uint8_t *)pvLast;
876 iPage = cPages;
877#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 19)
878 if (!fLinearMapping)
879 {
880 while (iPage-- > 0)
881 {
882 struct page *pPage = vmalloc_to_page(pbPage);
883 if (RT_UNLIKELY(!pPage))
884 {
885 rc = VERR_LOCK_FAILED;
886 break;
887 }
888 pMemLnx->apPages[iPage] = pPage;
889 pbPage -= PAGE_SIZE;
890 }
891 }
892 else
893#endif
894 {
895 while (iPage-- > 0)
896 {
897 pMemLnx->apPages[iPage] = virt_to_page(pbPage);
898 pbPage -= PAGE_SIZE;
899 }
900 }
901 if (RT_SUCCESS(rc))
902 {
903 /*
904 * Complete the memory object and return.
905 */
906 pMemLnx->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
907 pMemLnx->cPages = cPages;
908 Assert(!pMemLnx->fMappedToRing0);
909 *ppMem = &pMemLnx->Core;
910
911 return VINF_SUCCESS;
912 }
913
914 rtR0MemObjDelete(&pMemLnx->Core);
915 return rc;
916}
917
918
919int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
920{
921#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
922 const size_t cPages = cb >> PAGE_SHIFT;
923 struct page *pDummyPage;
924 struct page **papPages;
925
926 /* check for unsupported stuff. */
927 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
928 if (uAlignment > PAGE_SIZE)
929 return VERR_NOT_SUPPORTED;
930
931 /*
932 * Allocate a dummy page and create a page pointer array for vmap such that
933 * the dummy page is mapped all over the reserved area.
934 */
935 pDummyPage = alloc_page(GFP_HIGHUSER);
936 if (!pDummyPage)
937 return VERR_NO_MEMORY;
938 papPages = RTMemAlloc(sizeof(*papPages) * cPages);
939 if (papPages)
940 {
941 void *pv;
942 size_t iPage = cPages;
943 while (iPage-- > 0)
944 papPages[iPage] = pDummyPage;
945# ifdef VM_MAP
946 pv = vmap(papPages, cPages, VM_MAP, PAGE_KERNEL_RO);
947# else
948 pv = vmap(papPages, cPages, VM_ALLOC, PAGE_KERNEL_RO);
949# endif
950 RTMemFree(papPages);
951 if (pv)
952 {
953 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
954 if (pMemLnx)
955 {
956 pMemLnx->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
957 pMemLnx->cPages = 1;
958 pMemLnx->apPages[0] = pDummyPage;
959 *ppMem = &pMemLnx->Core;
960 return VINF_SUCCESS;
961 }
962 vunmap(pv);
963 }
964 }
965 __free_page(pDummyPage);
966 return VERR_NO_MEMORY;
967
968#else /* < 2.4.22 */
969 /*
970 * Could probably use ioremap here, but the caller is in a better position than us
971 * to select some safe physical memory.
972 */
973 return VERR_NOT_SUPPORTED;
974#endif
975}
976
977
978/**
979 * Worker for rtR0MemObjNativeReserveUser and rtR0MemObjNativerMapUser that creates
980 * an empty user space mapping.
981 *
982 * The caller takes care of acquiring the mmap_sem of the task.
983 *
984 * @returns Pointer to the mapping.
985 * (void *)-1 on failure.
986 * @param R3PtrFixed (RTR3PTR)-1 if anywhere, otherwise a specific location.
987 * @param cb The size of the mapping.
988 * @param uAlignment The alignment of the mapping.
989 * @param pTask The Linux task to create this mapping in.
990 * @param fProt The RTMEM_PROT_* mask.
991 */
992static void *rtR0MemObjLinuxDoMmap(RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, struct task_struct *pTask, unsigned fProt)
993{
994 unsigned fLnxProt;
995 unsigned long ulAddr;
996
997 /*
998 * Convert from IPRT protection to mman.h PROT_ and call do_mmap.
999 */
1000 fProt &= (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
1001 if (fProt == RTMEM_PROT_NONE)
1002 fLnxProt = PROT_NONE;
1003 else
1004 {
1005 fLnxProt = 0;
1006 if (fProt & RTMEM_PROT_READ)
1007 fLnxProt |= PROT_READ;
1008 if (fProt & RTMEM_PROT_WRITE)
1009 fLnxProt |= PROT_WRITE;
1010 if (fProt & RTMEM_PROT_EXEC)
1011 fLnxProt |= PROT_EXEC;
1012 }
1013
1014 if (R3PtrFixed != (RTR3PTR)-1)
1015 ulAddr = do_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
1016 else
1017 {
1018 ulAddr = do_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
1019 if ( !(ulAddr & ~PAGE_MASK)
1020 && (ulAddr & (uAlignment - 1)))
1021 {
1022 /** @todo implement uAlignment properly... We'll probably need to make some dummy mappings to fill
1023 * up alignment gaps. This is of course complicated by fragmentation (which we might have cause
1024 * ourselves) and further by there begin two mmap strategies (top / bottom). */
1025 /* For now, just ignore uAlignment requirements... */
1026 }
1027 }
1028 if (ulAddr & ~PAGE_MASK) /* ~PAGE_MASK == PAGE_OFFSET_MASK */
1029 return (void *)-1;
1030 return (void *)ulAddr;
1031}
1032
1033
1034int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
1035{
1036 PRTR0MEMOBJLNX pMemLnx;
1037 void *pv;
1038 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1039 if (!pTask)
1040 return VERR_NOT_SUPPORTED;
1041
1042 /*
1043 * Check that the specified alignment is supported.
1044 */
1045 if (uAlignment > PAGE_SIZE)
1046 return VERR_NOT_SUPPORTED;
1047
1048 /*
1049 * Let rtR0MemObjLinuxDoMmap do the difficult bits.
1050 */
1051 down_write(&pTask->mm->mmap_sem);
1052 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cb, uAlignment, pTask, RTMEM_PROT_NONE);
1053 up_write(&pTask->mm->mmap_sem);
1054 if (pv == (void *)-1)
1055 return VERR_NO_MEMORY;
1056
1057 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
1058 if (!pMemLnx)
1059 {
1060 down_write(&pTask->mm->mmap_sem);
1061 MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, cb);
1062 up_write(&pTask->mm->mmap_sem);
1063 return VERR_NO_MEMORY;
1064 }
1065
1066 pMemLnx->Core.u.ResVirt.R0Process = R0Process;
1067 *ppMem = &pMemLnx->Core;
1068 return VINF_SUCCESS;
1069}
1070
1071
1072int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
1073 unsigned fProt, size_t offSub, size_t cbSub)
1074{
1075 int rc = VERR_NO_MEMORY;
1076 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1077 PRTR0MEMOBJLNX pMemLnx;
1078
1079 /* Fail if requested to do something we can't. */
1080 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
1081 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
1082 if (uAlignment > PAGE_SIZE)
1083 return VERR_NOT_SUPPORTED;
1084
1085 /*
1086 * Create the IPRT memory object.
1087 */
1088 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1089 if (pMemLnx)
1090 {
1091 if (pMemLnxToMap->cPages)
1092 {
1093#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
1094 /*
1095 * Use vmap - 2.4.22 and later.
1096 */
1097 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, true /* kernel */);
1098# ifdef VM_MAP
1099 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_MAP, fPg);
1100# else
1101 pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_ALLOC, fPg);
1102# endif
1103 if (pMemLnx->Core.pv)
1104 {
1105 pMemLnx->fMappedToRing0 = true;
1106 rc = VINF_SUCCESS;
1107 }
1108 else
1109 rc = VERR_MAP_FAILED;
1110
1111#else /* < 2.4.22 */
1112 /*
1113 * Only option here is to share mappings if possible and forget about fProt.
1114 */
1115 if (rtR0MemObjIsRing3(pMemToMap))
1116 rc = VERR_NOT_SUPPORTED;
1117 else
1118 {
1119 rc = VINF_SUCCESS;
1120 if (!pMemLnxToMap->Core.pv)
1121 rc = rtR0MemObjLinuxVMap(pMemLnxToMap, !!(fProt & RTMEM_PROT_EXEC));
1122 if (RT_SUCCESS(rc))
1123 {
1124 Assert(pMemLnxToMap->Core.pv);
1125 pMemLnx->Core.pv = pMemLnxToMap->Core.pv;
1126 }
1127 }
1128#endif
1129 }
1130 else
1131 {
1132 /*
1133 * MMIO / physical memory.
1134 */
1135 Assert(pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS && !pMemLnxToMap->Core.u.Phys.fAllocated);
1136 pMemLnx->Core.pv = ioremap(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb);
1137 if (pMemLnx->Core.pv)
1138 {
1139 /** @todo fix protection. */
1140 rc = VINF_SUCCESS;
1141 }
1142 }
1143 if (RT_SUCCESS(rc))
1144 {
1145 pMemLnx->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
1146 *ppMem = &pMemLnx->Core;
1147 return VINF_SUCCESS;
1148 }
1149 rtR0MemObjDelete(&pMemLnx->Core);
1150 }
1151
1152 return rc;
1153}
1154
1155
1156#ifdef VBOX_USE_PAE_HACK
1157/**
1158 * Replace the PFN of a PTE with the address of the actual page.
1159 *
1160 * The caller maps a reserved dummy page at the address with the desired access
1161 * and flags.
1162 *
1163 * This hack is required for older Linux kernels which don't provide
1164 * remap_pfn_range().
1165 *
1166 * @returns 0 on success, -ENOMEM on failure.
1167 * @param mm The memory context.
1168 * @param ulAddr The mapping address.
1169 * @param Phys The physical address of the page to map.
1170 */
1171static int rtR0MemObjLinuxFixPte(struct mm_struct *mm, unsigned long ulAddr, RTHCPHYS Phys)
1172{
1173 int rc = -ENOMEM;
1174 pgd_t *pgd;
1175
1176 spin_lock(&mm->page_table_lock);
1177
1178 pgd = pgd_offset(mm, ulAddr);
1179 if (!pgd_none(*pgd) && !pgd_bad(*pgd))
1180 {
1181 pmd_t *pmd = pmd_offset(pgd, ulAddr);
1182 if (!pmd_none(*pmd))
1183 {
1184 pte_t *ptep = pte_offset_map(pmd, ulAddr);
1185 if (ptep)
1186 {
1187 pte_t pte = *ptep;
1188 pte.pte_high &= 0xfff00000;
1189 pte.pte_high |= ((Phys >> 32) & 0x000fffff);
1190 pte.pte_low &= 0x00000fff;
1191 pte.pte_low |= (Phys & 0xfffff000);
1192 set_pte(ptep, pte);
1193 pte_unmap(ptep);
1194 rc = 0;
1195 }
1196 }
1197 }
1198
1199 spin_unlock(&mm->page_table_lock);
1200 return rc;
1201}
1202#endif /* VBOX_USE_PAE_HACK */
1203
1204
1205int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
1206{
1207 struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
1208 PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
1209 int rc = VERR_NO_MEMORY;
1210 PRTR0MEMOBJLNX pMemLnx;
1211#ifdef VBOX_USE_PAE_HACK
1212 struct page *pDummyPage;
1213 RTHCPHYS DummyPhys;
1214#endif
1215
1216 /*
1217 * Check for restrictions.
1218 */
1219 if (!pTask)
1220 return VERR_NOT_SUPPORTED;
1221 if (uAlignment > PAGE_SIZE)
1222 return VERR_NOT_SUPPORTED;
1223
1224#ifdef VBOX_USE_PAE_HACK
1225 /*
1226 * Allocate a dummy page for use when mapping the memory.
1227 */
1228 pDummyPage = alloc_page(GFP_USER);
1229 if (!pDummyPage)
1230 return VERR_NO_MEMORY;
1231 SetPageReserved(pDummyPage);
1232 DummyPhys = page_to_phys(pDummyPage);
1233#endif
1234
1235 /*
1236 * Create the IPRT memory object.
1237 */
1238 pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
1239 if (pMemLnx)
1240 {
1241 /*
1242 * Allocate user space mapping.
1243 */
1244 void *pv;
1245 down_write(&pTask->mm->mmap_sem);
1246 pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, pMemLnxToMap->Core.cb, uAlignment, pTask, fProt);
1247 if (pv != (void *)-1)
1248 {
1249 /*
1250 * Map page by page into the mmap area.
1251 * This is generic, paranoid and not very efficient.
1252 */
1253 pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, false /* user */);
1254 unsigned long ulAddrCur = (unsigned long)pv;
1255 const size_t cPages = pMemLnxToMap->Core.cb >> PAGE_SHIFT;
1256 size_t iPage;
1257
1258 rc = 0;
1259 if (pMemLnxToMap->cPages)
1260 {
1261 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
1262 {
1263#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
1264 RTHCPHYS Phys = page_to_phys(pMemLnxToMap->apPages[iPage]);
1265#endif
1266#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1267 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1268 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1269#endif
1270#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_X86)
1271 /* remap_page_range() limitation on x86 */
1272 AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
1273#endif
1274
1275#if defined(VBOX_USE_INSERT_PAGE) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
1276 rc = vm_insert_page(vma, ulAddrCur, pMemLnxToMap->apPages[iPage]);
1277 vma->vm_flags |= VM_RESERVED; /* This flag helps making 100% sure some bad stuff wont happen (swap, core, ++). */
1278#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1279 rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
1280#elif defined(VBOX_USE_PAE_HACK)
1281 rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
1282 if (!rc)
1283 rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
1284#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1285 rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1286#else /* 2.4 */
1287 rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
1288#endif
1289 if (rc)
1290 {
1291 rc = VERR_NO_MEMORY;
1292 break;
1293 }
1294 }
1295 }
1296 else
1297 {
1298 RTHCPHYS Phys;
1299 if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS)
1300 Phys = pMemLnxToMap->Core.u.Phys.PhysBase;
1301 else if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_CONT)
1302 Phys = pMemLnxToMap->Core.u.Cont.Phys;
1303 else
1304 {
1305 AssertMsgFailed(("%d\n", pMemLnxToMap->Core.enmType));
1306 Phys = NIL_RTHCPHYS;
1307 }
1308 if (Phys != NIL_RTHCPHYS)
1309 {
1310 for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE, Phys += PAGE_SIZE)
1311 {
1312#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1313 struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
1314 AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
1315#endif
1316#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_X86)
1317 /* remap_page_range() limitation on x86 */
1318 AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
1319#endif
1320
1321#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1322 rc = remap_pfn_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1323#elif defined(VBOX_USE_PAE_HACK)
1324 rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
1325 if (!rc)
1326 rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
1327#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
1328 rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
1329#else /* 2.4 */
1330 rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
1331#endif
1332 if (rc)
1333 {
1334 rc = VERR_NO_MEMORY;
1335 break;
1336 }
1337 }
1338 }
1339 }
1340 if (!rc)
1341 {
1342 up_write(&pTask->mm->mmap_sem);
1343#ifdef VBOX_USE_PAE_HACK
1344 __free_page(pDummyPage);
1345#endif
1346
1347 pMemLnx->Core.pv = pv;
1348 pMemLnx->Core.u.Mapping.R0Process = R0Process;
1349 *ppMem = &pMemLnx->Core;
1350 return VINF_SUCCESS;
1351 }
1352
1353 /*
1354 * Bail out.
1355 */
1356 MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, pMemLnxToMap->Core.cb);
1357 }
1358 up_write(&pTask->mm->mmap_sem);
1359 rtR0MemObjDelete(&pMemLnx->Core);
1360 }
1361#ifdef VBOX_USE_PAE_HACK
1362 __free_page(pDummyPage);
1363#endif
1364
1365 return rc;
1366}
1367
1368
1369int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
1370{
1371 NOREF(pMem);
1372 NOREF(offSub);
1373 NOREF(cbSub);
1374 NOREF(fProt);
1375 return VERR_NOT_SUPPORTED;
1376}
1377
1378
1379RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
1380{
1381 PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
1382
1383 if (pMemLnx->cPages)
1384 return page_to_phys(pMemLnx->apPages[iPage]);
1385
1386 switch (pMemLnx->Core.enmType)
1387 {
1388 case RTR0MEMOBJTYPE_CONT:
1389 return pMemLnx->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
1390
1391 case RTR0MEMOBJTYPE_PHYS:
1392 return pMemLnx->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
1393
1394 /* the parent knows */
1395 case RTR0MEMOBJTYPE_MAPPING:
1396 return rtR0MemObjNativeGetPagePhysAddr(pMemLnx->Core.uRel.Child.pParent, iPage);
1397
1398 /* cPages > 0 */
1399 case RTR0MEMOBJTYPE_LOW:
1400 case RTR0MEMOBJTYPE_LOCK:
1401 case RTR0MEMOBJTYPE_PHYS_NC:
1402 case RTR0MEMOBJTYPE_PAGE:
1403 default:
1404 AssertMsgFailed(("%d\n", pMemLnx->Core.enmType));
1405 /* fall thru */
1406
1407 case RTR0MEMOBJTYPE_RES_VIRT:
1408 return NIL_RTHCPHYS;
1409 }
1410}
1411
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