VirtualBox

Ticket #9159: memobj-r0drv-linux.c

File memobj-r0drv-linux.c, 47.8 KB (added by Martin Cockerell, 13 years ago)

memobj-r0drv-linux.c with added printk calls for debugging

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

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