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