VirtualBox

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

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

Don't pass uAlignment=0 to rtR0MemObjNativeAllocPhys, resolve the alias like is done for the other APIs.

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