1 | /* $Revision: 36360 $ */
|
---|
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 | */
|
---|
75 | typedef 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 |
|
---|
91 | static 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 | */
|
---|
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 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 | */
|
---|
183 | static 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 | */
|
---|
313 | static 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 | */
|
---|
362 | static 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 | */
|
---|
429 | static 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 |
|
---|
445 | int 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 | if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
|
---|
489 | {
|
---|
490 | struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
|
---|
491 | Assert(pTask);
|
---|
492 | if (pTask && pTask->mm)
|
---|
493 | {
|
---|
494 | down_write(&pTask->mm->mmap_sem);
|
---|
495 | MY_DO_MUNMAP(pTask->mm, (unsigned long)pMemLnx->Core.pv, pMemLnx->Core.cb);
|
---|
496 | up_write(&pTask->mm->mmap_sem);
|
---|
497 | }
|
---|
498 | }
|
---|
499 | else
|
---|
500 | {
|
---|
501 | vunmap(pMemLnx->Core.pv);
|
---|
502 |
|
---|
503 | Assert(pMemLnx->cPages == 1 && pMemLnx->apPages[0] != NULL);
|
---|
504 | __free_page(pMemLnx->apPages[0]);
|
---|
505 | pMemLnx->apPages[0] = NULL;
|
---|
506 | pMemLnx->cPages = 0;
|
---|
507 | }
|
---|
508 | pMemLnx->Core.pv = NULL;
|
---|
509 | break;
|
---|
510 |
|
---|
511 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
512 | Assert(pMemLnx->cPages == 0); Assert(pMemLnx->Core.pv);
|
---|
513 | if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
|
---|
514 | {
|
---|
515 | struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
|
---|
516 | Assert(pTask);
|
---|
517 | if (pTask && pTask->mm)
|
---|
518 | {
|
---|
519 | down_write(&pTask->mm->mmap_sem);
|
---|
520 | MY_DO_MUNMAP(pTask->mm, (unsigned long)pMemLnx->Core.pv, pMemLnx->Core.cb);
|
---|
521 | up_write(&pTask->mm->mmap_sem);
|
---|
522 | }
|
---|
523 | }
|
---|
524 | else
|
---|
525 | vunmap(pMemLnx->Core.pv);
|
---|
526 | pMemLnx->Core.pv = NULL;
|
---|
527 | break;
|
---|
528 |
|
---|
529 | default:
|
---|
530 | AssertMsgFailed(("enmType=%d\n", pMemLnx->Core.enmType));
|
---|
531 | return VERR_INTERNAL_ERROR;
|
---|
532 | }
|
---|
533 | return VINF_SUCCESS;
|
---|
534 | }
|
---|
535 |
|
---|
536 |
|
---|
537 | int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
538 | {
|
---|
539 | PRTR0MEMOBJLNX pMemLnx;
|
---|
540 | int rc;
|
---|
541 |
|
---|
542 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
543 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, PAGE_SIZE, GFP_HIGHUSER, false /* non-contiguous */);
|
---|
544 | #else
|
---|
545 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, PAGE_SIZE, GFP_USER, false /* non-contiguous */);
|
---|
546 | #endif
|
---|
547 | if (RT_SUCCESS(rc))
|
---|
548 | {
|
---|
549 | rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
|
---|
550 | if (RT_SUCCESS(rc))
|
---|
551 | {
|
---|
552 | *ppMem = &pMemLnx->Core;
|
---|
553 | return rc;
|
---|
554 | }
|
---|
555 |
|
---|
556 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
557 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
558 | }
|
---|
559 |
|
---|
560 | return rc;
|
---|
561 | }
|
---|
562 |
|
---|
563 |
|
---|
564 | int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
565 | {
|
---|
566 | PRTR0MEMOBJLNX pMemLnx;
|
---|
567 | int rc;
|
---|
568 |
|
---|
569 | /* Try to avoid GFP_DMA. GFM_DMA32 was introduced with Linux 2.6.15. */
|
---|
570 | #if (defined(RT_ARCH_AMD64) || defined(CONFIG_X86_PAE)) && defined(GFP_DMA32)
|
---|
571 | /* ZONE_DMA32: 0-4GB */
|
---|
572 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, PAGE_SIZE, GFP_DMA32, false /* non-contiguous */);
|
---|
573 | if (RT_FAILURE(rc))
|
---|
574 | #endif
|
---|
575 | #ifdef RT_ARCH_AMD64
|
---|
576 | /* ZONE_DMA: 0-16MB */
|
---|
577 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, PAGE_SIZE, GFP_DMA, false /* non-contiguous */);
|
---|
578 | #else
|
---|
579 | # ifdef CONFIG_X86_PAE
|
---|
580 | # endif
|
---|
581 | /* ZONE_NORMAL: 0-896MB */
|
---|
582 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, PAGE_SIZE, GFP_USER, false /* non-contiguous */);
|
---|
583 | #endif
|
---|
584 | if (RT_SUCCESS(rc))
|
---|
585 | {
|
---|
586 | rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
|
---|
587 | if (RT_SUCCESS(rc))
|
---|
588 | {
|
---|
589 | *ppMem = &pMemLnx->Core;
|
---|
590 | return rc;
|
---|
591 | }
|
---|
592 |
|
---|
593 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
594 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
595 | }
|
---|
596 |
|
---|
597 | return rc;
|
---|
598 | }
|
---|
599 |
|
---|
600 |
|
---|
601 | int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
602 | {
|
---|
603 | PRTR0MEMOBJLNX pMemLnx;
|
---|
604 | int rc;
|
---|
605 |
|
---|
606 | #if (defined(RT_ARCH_AMD64) || defined(CONFIG_X86_PAE)) && defined(GFP_DMA32)
|
---|
607 | /* ZONE_DMA32: 0-4GB */
|
---|
608 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, PAGE_SIZE, GFP_DMA32, true /* contiguous */);
|
---|
609 | if (RT_FAILURE(rc))
|
---|
610 | #endif
|
---|
611 | #ifdef RT_ARCH_AMD64
|
---|
612 | /* ZONE_DMA: 0-16MB */
|
---|
613 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, PAGE_SIZE, GFP_DMA, true /* contiguous */);
|
---|
614 | #else
|
---|
615 | /* ZONE_NORMAL (32-bit hosts): 0-896MB */
|
---|
616 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, PAGE_SIZE, GFP_USER, true /* contiguous */);
|
---|
617 | #endif
|
---|
618 | if (RT_SUCCESS(rc))
|
---|
619 | {
|
---|
620 | rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
|
---|
621 | if (RT_SUCCESS(rc))
|
---|
622 | {
|
---|
623 | #if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(CONFIG_HIGHMEM64G))
|
---|
624 | size_t iPage = pMemLnx->cPages;
|
---|
625 | while (iPage-- > 0)
|
---|
626 | Assert(page_to_phys(pMemLnx->apPages[iPage]) < _4G);
|
---|
627 | #endif
|
---|
628 | pMemLnx->Core.u.Cont.Phys = page_to_phys(pMemLnx->apPages[0]);
|
---|
629 | *ppMem = &pMemLnx->Core;
|
---|
630 | return rc;
|
---|
631 | }
|
---|
632 |
|
---|
633 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
634 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
635 | }
|
---|
636 |
|
---|
637 | return rc;
|
---|
638 | }
|
---|
639 |
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * Worker for rtR0MemObjLinuxAllocPhysSub that tries one allocation strategy.
|
---|
643 | *
|
---|
644 | * @returns IPRT status.
|
---|
645 | * @param ppMemLnx Where to
|
---|
646 | * @param enmType The object type.
|
---|
647 | * @param cb The size of the allocation.
|
---|
648 | * @param uAlignment The alignment of the physical memory.
|
---|
649 | * Only valid for fContiguous == true, ignored otherwise.
|
---|
650 | * @param PhysHighest See rtR0MemObjNativeAllocPhys.
|
---|
651 | * @param fGfp The Linux GFP flags to use for the allocation.
|
---|
652 | */
|
---|
653 | static int rtR0MemObjLinuxAllocPhysSub2(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType,
|
---|
654 | size_t cb, size_t uAlignment, RTHCPHYS PhysHighest, unsigned fGfp)
|
---|
655 | {
|
---|
656 | PRTR0MEMOBJLNX pMemLnx;
|
---|
657 | int rc;
|
---|
658 |
|
---|
659 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, enmType, cb, uAlignment, fGfp,
|
---|
660 | enmType == RTR0MEMOBJTYPE_PHYS /* contiguous / non-contiguous */);
|
---|
661 | if (RT_FAILURE(rc))
|
---|
662 | return rc;
|
---|
663 |
|
---|
664 | /*
|
---|
665 | * Check the addresses if necessary. (Can be optimized a bit for PHYS.)
|
---|
666 | */
|
---|
667 | if (PhysHighest != NIL_RTHCPHYS)
|
---|
668 | {
|
---|
669 | size_t iPage = pMemLnx->cPages;
|
---|
670 | while (iPage-- > 0)
|
---|
671 | if (page_to_phys(pMemLnx->apPages[iPage]) >= PhysHighest)
|
---|
672 | {
|
---|
673 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
674 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
675 | return VERR_NO_MEMORY;
|
---|
676 | }
|
---|
677 | }
|
---|
678 |
|
---|
679 | /*
|
---|
680 | * Complete the object.
|
---|
681 | */
|
---|
682 | if (enmType == RTR0MEMOBJTYPE_PHYS)
|
---|
683 | {
|
---|
684 | pMemLnx->Core.u.Phys.PhysBase = page_to_phys(pMemLnx->apPages[0]);
|
---|
685 | pMemLnx->Core.u.Phys.fAllocated = true;
|
---|
686 | }
|
---|
687 | *ppMem = &pMemLnx->Core;
|
---|
688 | return rc;
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Worker for rtR0MemObjNativeAllocPhys and rtR0MemObjNativeAllocPhysNC.
|
---|
694 | *
|
---|
695 | * @returns IPRT status.
|
---|
696 | * @param ppMem Where to store the memory object pointer on success.
|
---|
697 | * @param enmType The object type.
|
---|
698 | * @param cb The size of the allocation.
|
---|
699 | * @param uAlignment The alignment of the physical memory.
|
---|
700 | * Only valid for enmType == RTR0MEMOBJTYPE_PHYS, ignored otherwise.
|
---|
701 | * @param PhysHighest See rtR0MemObjNativeAllocPhys.
|
---|
702 | */
|
---|
703 | static int rtR0MemObjLinuxAllocPhysSub(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType,
|
---|
704 | size_t cb, size_t uAlignment, RTHCPHYS PhysHighest)
|
---|
705 | {
|
---|
706 | int rc;
|
---|
707 |
|
---|
708 | /*
|
---|
709 | * There are two clear cases and that's the <=16MB and anything-goes ones.
|
---|
710 | * When the physical address limit is somewhere in-between those two we'll
|
---|
711 | * just have to try, starting with HIGHUSER and working our way thru the
|
---|
712 | * different types, hoping we'll get lucky.
|
---|
713 | *
|
---|
714 | * We should probably move this physical address restriction logic up to
|
---|
715 | * the page alloc function as it would be more efficient there. But since
|
---|
716 | * we don't expect this to be a performance issue just yet it can wait.
|
---|
717 | */
|
---|
718 | if (PhysHighest == NIL_RTHCPHYS)
|
---|
719 | /* ZONE_HIGHMEM: the whole physical memory */
|
---|
720 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_HIGHUSER);
|
---|
721 | else if (PhysHighest <= _1M * 16)
|
---|
722 | /* ZONE_DMA: 0-16MB */
|
---|
723 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_DMA);
|
---|
724 | else
|
---|
725 | {
|
---|
726 | rc = VERR_NO_MEMORY;
|
---|
727 | if (RT_FAILURE(rc))
|
---|
728 | /* ZONE_HIGHMEM: the whole physical memory */
|
---|
729 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_HIGHUSER);
|
---|
730 | if (RT_FAILURE(rc))
|
---|
731 | /* ZONE_NORMAL: 0-896MB */
|
---|
732 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_USER);
|
---|
733 | #ifdef GFP_DMA32
|
---|
734 | if (RT_FAILURE(rc))
|
---|
735 | /* ZONE_DMA32: 0-4GB */
|
---|
736 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_DMA32);
|
---|
737 | #endif
|
---|
738 | if (RT_FAILURE(rc))
|
---|
739 | /* ZONE_DMA: 0-16MB */
|
---|
740 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_DMA);
|
---|
741 | }
|
---|
742 | return rc;
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 | int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
|
---|
747 | {
|
---|
748 | return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS, cb, uAlignment, PhysHighest);
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
753 | {
|
---|
754 | return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS_NC, cb, PAGE_SIZE, PhysHighest);
|
---|
755 | }
|
---|
756 |
|
---|
757 |
|
---|
758 | int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
|
---|
759 | {
|
---|
760 | /*
|
---|
761 | * All we need to do here is to validate that we can use
|
---|
762 | * ioremap on the specified address (32/64-bit dma_addr_t).
|
---|
763 | */
|
---|
764 | PRTR0MEMOBJLNX pMemLnx;
|
---|
765 | dma_addr_t PhysAddr = Phys;
|
---|
766 | AssertMsgReturn(PhysAddr == Phys, ("%#llx\n", (unsigned long long)Phys), VERR_ADDRESS_TOO_BIG);
|
---|
767 |
|
---|
768 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
769 | if (!pMemLnx)
|
---|
770 | return VERR_NO_MEMORY;
|
---|
771 |
|
---|
772 | pMemLnx->Core.u.Phys.PhysBase = PhysAddr;
|
---|
773 | pMemLnx->Core.u.Phys.fAllocated = false;
|
---|
774 | pMemLnx->Core.u.Phys.uCachePolicy = uCachePolicy;
|
---|
775 | Assert(!pMemLnx->cPages);
|
---|
776 | *ppMem = &pMemLnx->Core;
|
---|
777 | return VINF_SUCCESS;
|
---|
778 | }
|
---|
779 |
|
---|
780 |
|
---|
781 | int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
|
---|
782 | {
|
---|
783 | const int cPages = cb >> PAGE_SHIFT;
|
---|
784 | struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
|
---|
785 | struct vm_area_struct **papVMAs;
|
---|
786 | PRTR0MEMOBJLNX pMemLnx;
|
---|
787 | int rc = VERR_NO_MEMORY;
|
---|
788 | NOREF(fAccess);
|
---|
789 |
|
---|
790 | /*
|
---|
791 | * Check for valid task and size overflows.
|
---|
792 | */
|
---|
793 | if (!pTask)
|
---|
794 | return VERR_NOT_SUPPORTED;
|
---|
795 | if (((size_t)cPages << PAGE_SHIFT) != cb)
|
---|
796 | return VERR_OUT_OF_RANGE;
|
---|
797 |
|
---|
798 | /*
|
---|
799 | * Allocate the memory object and a temporary buffer for the VMAs.
|
---|
800 | */
|
---|
801 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
|
---|
802 | if (!pMemLnx)
|
---|
803 | return VERR_NO_MEMORY;
|
---|
804 |
|
---|
805 | papVMAs = (struct vm_area_struct **)RTMemAlloc(sizeof(*papVMAs) * cPages);
|
---|
806 | if (papVMAs)
|
---|
807 | {
|
---|
808 | down_read(&pTask->mm->mmap_sem);
|
---|
809 |
|
---|
810 | /*
|
---|
811 | * Get user pages.
|
---|
812 | */
|
---|
813 | rc = get_user_pages(pTask, /* Task for fault accounting. */
|
---|
814 | pTask->mm, /* Whose pages. */
|
---|
815 | R3Ptr, /* Where from. */
|
---|
816 | cPages, /* How many pages. */
|
---|
817 | 1, /* Write to memory. */
|
---|
818 | 0, /* force. */
|
---|
819 | &pMemLnx->apPages[0], /* Page array. */
|
---|
820 | papVMAs); /* vmas */
|
---|
821 | if (rc == cPages)
|
---|
822 | {
|
---|
823 | /*
|
---|
824 | * Flush dcache (required?), protect against fork and _really_ pin the page
|
---|
825 | * table entries. get_user_pages() will protect against swapping out the
|
---|
826 | * pages but it will NOT protect against removing page table entries. This
|
---|
827 | * can be achieved with
|
---|
828 | * - using mlock / mmap(..., MAP_LOCKED, ...) from userland. This requires
|
---|
829 | * an appropriate limit set up with setrlimit(..., RLIMIT_MEMLOCK, ...).
|
---|
830 | * Usual Linux distributions support only a limited size of locked pages
|
---|
831 | * (e.g. 32KB).
|
---|
832 | * - setting the PageReserved bit (as we do in rtR0MemObjLinuxAllocPages()
|
---|
833 | * or by
|
---|
834 | * - setting the VM_LOCKED flag. This is the same as doing mlock() without
|
---|
835 | * a range check.
|
---|
836 | */
|
---|
837 | /** @todo The Linux fork() protection will require more work if this API
|
---|
838 | * is to be used for anything but locking VM pages. */
|
---|
839 | while (rc-- > 0)
|
---|
840 | {
|
---|
841 | flush_dcache_page(pMemLnx->apPages[rc]);
|
---|
842 | papVMAs[rc]->vm_flags |= (VM_DONTCOPY | VM_LOCKED);
|
---|
843 | }
|
---|
844 |
|
---|
845 | up_read(&pTask->mm->mmap_sem);
|
---|
846 |
|
---|
847 | RTMemFree(papVMAs);
|
---|
848 |
|
---|
849 | pMemLnx->Core.u.Lock.R0Process = R0Process;
|
---|
850 | pMemLnx->cPages = cPages;
|
---|
851 | Assert(!pMemLnx->fMappedToRing0);
|
---|
852 | *ppMem = &pMemLnx->Core;
|
---|
853 |
|
---|
854 | return VINF_SUCCESS;
|
---|
855 | }
|
---|
856 |
|
---|
857 | /*
|
---|
858 | * Failed - we need to unlock any pages that we succeeded to lock.
|
---|
859 | */
|
---|
860 | while (rc-- > 0)
|
---|
861 | {
|
---|
862 | if (!PageReserved(pMemLnx->apPages[rc]))
|
---|
863 | SetPageDirty(pMemLnx->apPages[rc]);
|
---|
864 | page_cache_release(pMemLnx->apPages[rc]);
|
---|
865 | }
|
---|
866 |
|
---|
867 | up_read(&pTask->mm->mmap_sem);
|
---|
868 |
|
---|
869 | RTMemFree(papVMAs);
|
---|
870 | rc = VERR_LOCK_FAILED;
|
---|
871 | }
|
---|
872 |
|
---|
873 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
874 | return rc;
|
---|
875 | }
|
---|
876 |
|
---|
877 |
|
---|
878 | int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
|
---|
879 | {
|
---|
880 | void *pvLast = (uint8_t *)pv + cb - 1;
|
---|
881 | size_t const cPages = cb >> PAGE_SHIFT;
|
---|
882 | PRTR0MEMOBJLNX pMemLnx;
|
---|
883 | bool fLinearMapping;
|
---|
884 | int rc;
|
---|
885 | uint8_t *pbPage;
|
---|
886 | size_t iPage;
|
---|
887 | NOREF(fAccess);
|
---|
888 |
|
---|
889 | /*
|
---|
890 | * Classify the memory and check that we can deal with it.
|
---|
891 | */
|
---|
892 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
893 | fLinearMapping = virt_addr_valid(pvLast) && virt_addr_valid(pv);
|
---|
894 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0)
|
---|
895 | fLinearMapping = VALID_PAGE(virt_to_page(pvLast)) && VALID_PAGE(virt_to_page(pv));
|
---|
896 | #else
|
---|
897 | # error "not supported"
|
---|
898 | #endif
|
---|
899 | /*
|
---|
900 | * kmap()'ed memory. Only relevant for 32-bit Linux kernels with HIGHMEM
|
---|
901 | * enabled. Unfortunately there is no easy way to retrieve the page object
|
---|
902 | * for such temporarily mapped memory, virt_to_page() does not work here.
|
---|
903 | * There is even no function to check if a virtual address is inside the
|
---|
904 | * kmap() area or not :-( kmap_atomic_to_page() looks promising but the test
|
---|
905 | * 'if (vaddr < FIXADDR_START)' if wrong -- the kmap() area is located
|
---|
906 | * below the fixmap area. vmalloc_to_page() would work but is only allowed
|
---|
907 | * for vmalloc'ed memory.
|
---|
908 | */
|
---|
909 | #ifdef CONFIG_HIGHMEM
|
---|
910 | if (pv < PKMAP_BASE + LAST_PKMAP*PAGE_SIZE && pvLast >= PKMAP_BASE)
|
---|
911 | return VERR_INVALID_PARAMETER;
|
---|
912 | #endif
|
---|
913 | if (!fLinearMapping)
|
---|
914 | {
|
---|
915 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 19)
|
---|
916 | if ( !RTR0MemKernelIsValidAddr(pv)
|
---|
917 | || !RTR0MemKernelIsValidAddr(pv + cb))
|
---|
918 | #endif
|
---|
919 | return VERR_INVALID_PARAMETER;
|
---|
920 | }
|
---|
921 |
|
---|
922 | /*
|
---|
923 | * Allocate the memory object.
|
---|
924 | */
|
---|
925 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, pv, cb);
|
---|
926 | if (!pMemLnx)
|
---|
927 | return VERR_NO_MEMORY;
|
---|
928 |
|
---|
929 | /*
|
---|
930 | * Gather the pages.
|
---|
931 | * We ASSUME all kernel pages are non-swappable.
|
---|
932 | */
|
---|
933 | rc = VINF_SUCCESS;
|
---|
934 | pbPage = (uint8_t *)pvLast;
|
---|
935 | iPage = cPages;
|
---|
936 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 19)
|
---|
937 | if (!fLinearMapping)
|
---|
938 | {
|
---|
939 | while (iPage-- > 0)
|
---|
940 | {
|
---|
941 | struct page *pPage = vmalloc_to_page(pbPage);
|
---|
942 | if (RT_UNLIKELY(!pPage))
|
---|
943 | {
|
---|
944 | rc = VERR_LOCK_FAILED;
|
---|
945 | break;
|
---|
946 | }
|
---|
947 | pMemLnx->apPages[iPage] = pPage;
|
---|
948 | pbPage -= PAGE_SIZE;
|
---|
949 | }
|
---|
950 | }
|
---|
951 | else
|
---|
952 | #endif
|
---|
953 | {
|
---|
954 | while (iPage-- > 0)
|
---|
955 | {
|
---|
956 | pMemLnx->apPages[iPage] = virt_to_page(pbPage);
|
---|
957 | pbPage -= PAGE_SIZE;
|
---|
958 | }
|
---|
959 | }
|
---|
960 | if (RT_SUCCESS(rc))
|
---|
961 | {
|
---|
962 | /*
|
---|
963 | * Complete the memory object and return.
|
---|
964 | */
|
---|
965 | pMemLnx->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
|
---|
966 | pMemLnx->cPages = cPages;
|
---|
967 | Assert(!pMemLnx->fMappedToRing0);
|
---|
968 | *ppMem = &pMemLnx->Core;
|
---|
969 |
|
---|
970 | return VINF_SUCCESS;
|
---|
971 | }
|
---|
972 |
|
---|
973 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
974 | return rc;
|
---|
975 | }
|
---|
976 |
|
---|
977 |
|
---|
978 | int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
|
---|
979 | {
|
---|
980 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
981 | const size_t cPages = cb >> PAGE_SHIFT;
|
---|
982 | struct page *pDummyPage;
|
---|
983 | struct page **papPages;
|
---|
984 |
|
---|
985 | /* check for unsupported stuff. */
|
---|
986 | AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
|
---|
987 | if (uAlignment > PAGE_SIZE)
|
---|
988 | return VERR_NOT_SUPPORTED;
|
---|
989 |
|
---|
990 | /*
|
---|
991 | * Allocate a dummy page and create a page pointer array for vmap such that
|
---|
992 | * the dummy page is mapped all over the reserved area.
|
---|
993 | */
|
---|
994 | pDummyPage = alloc_page(GFP_HIGHUSER);
|
---|
995 | if (!pDummyPage)
|
---|
996 | return VERR_NO_MEMORY;
|
---|
997 | papPages = RTMemAlloc(sizeof(*papPages) * cPages);
|
---|
998 | if (papPages)
|
---|
999 | {
|
---|
1000 | void *pv;
|
---|
1001 | size_t iPage = cPages;
|
---|
1002 | while (iPage-- > 0)
|
---|
1003 | papPages[iPage] = pDummyPage;
|
---|
1004 | # ifdef VM_MAP
|
---|
1005 | pv = vmap(papPages, cPages, VM_MAP, PAGE_KERNEL_RO);
|
---|
1006 | # else
|
---|
1007 | pv = vmap(papPages, cPages, VM_ALLOC, PAGE_KERNEL_RO);
|
---|
1008 | # endif
|
---|
1009 | RTMemFree(papPages);
|
---|
1010 | if (pv)
|
---|
1011 | {
|
---|
1012 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
|
---|
1013 | if (pMemLnx)
|
---|
1014 | {
|
---|
1015 | pMemLnx->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
|
---|
1016 | pMemLnx->cPages = 1;
|
---|
1017 | pMemLnx->apPages[0] = pDummyPage;
|
---|
1018 | *ppMem = &pMemLnx->Core;
|
---|
1019 | return VINF_SUCCESS;
|
---|
1020 | }
|
---|
1021 | vunmap(pv);
|
---|
1022 | }
|
---|
1023 | }
|
---|
1024 | __free_page(pDummyPage);
|
---|
1025 | return VERR_NO_MEMORY;
|
---|
1026 |
|
---|
1027 | #else /* < 2.4.22 */
|
---|
1028 | /*
|
---|
1029 | * Could probably use ioremap here, but the caller is in a better position than us
|
---|
1030 | * to select some safe physical memory.
|
---|
1031 | */
|
---|
1032 | return VERR_NOT_SUPPORTED;
|
---|
1033 | #endif
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 |
|
---|
1037 | /**
|
---|
1038 | * Worker for rtR0MemObjNativeReserveUser and rtR0MemObjNativerMapUser that creates
|
---|
1039 | * an empty user space mapping.
|
---|
1040 | *
|
---|
1041 | * The caller takes care of acquiring the mmap_sem of the task.
|
---|
1042 | *
|
---|
1043 | * @returns Pointer to the mapping.
|
---|
1044 | * (void *)-1 on failure.
|
---|
1045 | * @param R3PtrFixed (RTR3PTR)-1 if anywhere, otherwise a specific location.
|
---|
1046 | * @param cb The size of the mapping.
|
---|
1047 | * @param uAlignment The alignment of the mapping.
|
---|
1048 | * @param pTask The Linux task to create this mapping in.
|
---|
1049 | * @param fProt The RTMEM_PROT_* mask.
|
---|
1050 | */
|
---|
1051 | static void *rtR0MemObjLinuxDoMmap(RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, struct task_struct *pTask, unsigned fProt)
|
---|
1052 | {
|
---|
1053 | unsigned fLnxProt;
|
---|
1054 | unsigned long ulAddr;
|
---|
1055 |
|
---|
1056 | /*
|
---|
1057 | * Convert from IPRT protection to mman.h PROT_ and call do_mmap.
|
---|
1058 | */
|
---|
1059 | fProt &= (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
|
---|
1060 | if (fProt == RTMEM_PROT_NONE)
|
---|
1061 | fLnxProt = PROT_NONE;
|
---|
1062 | else
|
---|
1063 | {
|
---|
1064 | fLnxProt = 0;
|
---|
1065 | if (fProt & RTMEM_PROT_READ)
|
---|
1066 | fLnxProt |= PROT_READ;
|
---|
1067 | if (fProt & RTMEM_PROT_WRITE)
|
---|
1068 | fLnxProt |= PROT_WRITE;
|
---|
1069 | if (fProt & RTMEM_PROT_EXEC)
|
---|
1070 | fLnxProt |= PROT_EXEC;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | if (R3PtrFixed != (RTR3PTR)-1)
|
---|
1074 | ulAddr = do_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
|
---|
1075 | else
|
---|
1076 | {
|
---|
1077 | ulAddr = do_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
|
---|
1078 | if ( !(ulAddr & ~PAGE_MASK)
|
---|
1079 | && (ulAddr & (uAlignment - 1)))
|
---|
1080 | {
|
---|
1081 | /** @todo implement uAlignment properly... We'll probably need to make some dummy mappings to fill
|
---|
1082 | * up alignment gaps. This is of course complicated by fragmentation (which we might have cause
|
---|
1083 | * ourselves) and further by there begin two mmap strategies (top / bottom). */
|
---|
1084 | /* For now, just ignore uAlignment requirements... */
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 | if (ulAddr & ~PAGE_MASK) /* ~PAGE_MASK == PAGE_OFFSET_MASK */
|
---|
1088 | return (void *)-1;
|
---|
1089 | return (void *)ulAddr;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 |
|
---|
1093 | int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
|
---|
1094 | {
|
---|
1095 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1096 | void *pv;
|
---|
1097 | struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
|
---|
1098 | if (!pTask)
|
---|
1099 | return VERR_NOT_SUPPORTED;
|
---|
1100 |
|
---|
1101 | /*
|
---|
1102 | * Check that the specified alignment is supported.
|
---|
1103 | */
|
---|
1104 | if (uAlignment > PAGE_SIZE)
|
---|
1105 | return VERR_NOT_SUPPORTED;
|
---|
1106 |
|
---|
1107 | /*
|
---|
1108 | * Let rtR0MemObjLinuxDoMmap do the difficult bits.
|
---|
1109 | */
|
---|
1110 | down_write(&pTask->mm->mmap_sem);
|
---|
1111 | pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cb, uAlignment, pTask, RTMEM_PROT_NONE);
|
---|
1112 | up_write(&pTask->mm->mmap_sem);
|
---|
1113 | if (pv == (void *)-1)
|
---|
1114 | return VERR_NO_MEMORY;
|
---|
1115 |
|
---|
1116 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
|
---|
1117 | if (!pMemLnx)
|
---|
1118 | {
|
---|
1119 | down_write(&pTask->mm->mmap_sem);
|
---|
1120 | MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, cb);
|
---|
1121 | up_write(&pTask->mm->mmap_sem);
|
---|
1122 | return VERR_NO_MEMORY;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | pMemLnx->Core.u.ResVirt.R0Process = R0Process;
|
---|
1126 | *ppMem = &pMemLnx->Core;
|
---|
1127 | return VINF_SUCCESS;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 |
|
---|
1131 | int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
|
---|
1132 | unsigned fProt, size_t offSub, size_t cbSub)
|
---|
1133 | {
|
---|
1134 | int rc = VERR_NO_MEMORY;
|
---|
1135 | PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
|
---|
1136 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1137 |
|
---|
1138 | /* Fail if requested to do something we can't. */
|
---|
1139 | AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
|
---|
1140 | AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
|
---|
1141 | if (uAlignment > PAGE_SIZE)
|
---|
1142 | return VERR_NOT_SUPPORTED;
|
---|
1143 |
|
---|
1144 | /*
|
---|
1145 | * Create the IPRT memory object.
|
---|
1146 | */
|
---|
1147 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
|
---|
1148 | if (pMemLnx)
|
---|
1149 | {
|
---|
1150 | if (pMemLnxToMap->cPages)
|
---|
1151 | {
|
---|
1152 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
1153 | /*
|
---|
1154 | * Use vmap - 2.4.22 and later.
|
---|
1155 | */
|
---|
1156 | pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, true /* kernel */);
|
---|
1157 | # ifdef VM_MAP
|
---|
1158 | pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_MAP, fPg);
|
---|
1159 | # else
|
---|
1160 | pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_ALLOC, fPg);
|
---|
1161 | # endif
|
---|
1162 | if (pMemLnx->Core.pv)
|
---|
1163 | {
|
---|
1164 | pMemLnx->fMappedToRing0 = true;
|
---|
1165 | rc = VINF_SUCCESS;
|
---|
1166 | }
|
---|
1167 | else
|
---|
1168 | rc = VERR_MAP_FAILED;
|
---|
1169 |
|
---|
1170 | #else /* < 2.4.22 */
|
---|
1171 | /*
|
---|
1172 | * Only option here is to share mappings if possible and forget about fProt.
|
---|
1173 | */
|
---|
1174 | if (rtR0MemObjIsRing3(pMemToMap))
|
---|
1175 | rc = VERR_NOT_SUPPORTED;
|
---|
1176 | else
|
---|
1177 | {
|
---|
1178 | rc = VINF_SUCCESS;
|
---|
1179 | if (!pMemLnxToMap->Core.pv)
|
---|
1180 | rc = rtR0MemObjLinuxVMap(pMemLnxToMap, !!(fProt & RTMEM_PROT_EXEC));
|
---|
1181 | if (RT_SUCCESS(rc))
|
---|
1182 | {
|
---|
1183 | Assert(pMemLnxToMap->Core.pv);
|
---|
1184 | pMemLnx->Core.pv = pMemLnxToMap->Core.pv;
|
---|
1185 | }
|
---|
1186 | }
|
---|
1187 | #endif
|
---|
1188 | }
|
---|
1189 | else
|
---|
1190 | {
|
---|
1191 | /*
|
---|
1192 | * MMIO / physical memory.
|
---|
1193 | */
|
---|
1194 | Assert(pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS && !pMemLnxToMap->Core.u.Phys.fAllocated);
|
---|
1195 | pMemLnx->Core.pv = pMemLnxToMap->Core.u.Phys.uCachePolicy == RTMEM_CACHE_POLICY_MMIO
|
---|
1196 | ? ioremap_nocache(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb)
|
---|
1197 | : ioremap(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb);
|
---|
1198 | if (pMemLnx->Core.pv)
|
---|
1199 | {
|
---|
1200 | /** @todo fix protection. */
|
---|
1201 | rc = VINF_SUCCESS;
|
---|
1202 | }
|
---|
1203 | }
|
---|
1204 | if (RT_SUCCESS(rc))
|
---|
1205 | {
|
---|
1206 | pMemLnx->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
|
---|
1207 | *ppMem = &pMemLnx->Core;
|
---|
1208 | return VINF_SUCCESS;
|
---|
1209 | }
|
---|
1210 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | return rc;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 |
|
---|
1217 | #ifdef VBOX_USE_PAE_HACK
|
---|
1218 | /**
|
---|
1219 | * Replace the PFN of a PTE with the address of the actual page.
|
---|
1220 | *
|
---|
1221 | * The caller maps a reserved dummy page at the address with the desired access
|
---|
1222 | * and flags.
|
---|
1223 | *
|
---|
1224 | * This hack is required for older Linux kernels which don't provide
|
---|
1225 | * remap_pfn_range().
|
---|
1226 | *
|
---|
1227 | * @returns 0 on success, -ENOMEM on failure.
|
---|
1228 | * @param mm The memory context.
|
---|
1229 | * @param ulAddr The mapping address.
|
---|
1230 | * @param Phys The physical address of the page to map.
|
---|
1231 | */
|
---|
1232 | static int rtR0MemObjLinuxFixPte(struct mm_struct *mm, unsigned long ulAddr, RTHCPHYS Phys)
|
---|
1233 | {
|
---|
1234 | int rc = -ENOMEM;
|
---|
1235 | pgd_t *pgd;
|
---|
1236 |
|
---|
1237 | spin_lock(&mm->page_table_lock);
|
---|
1238 |
|
---|
1239 | pgd = pgd_offset(mm, ulAddr);
|
---|
1240 | if (!pgd_none(*pgd) && !pgd_bad(*pgd))
|
---|
1241 | {
|
---|
1242 | pmd_t *pmd = pmd_offset(pgd, ulAddr);
|
---|
1243 | if (!pmd_none(*pmd))
|
---|
1244 | {
|
---|
1245 | pte_t *ptep = pte_offset_map(pmd, ulAddr);
|
---|
1246 | if (ptep)
|
---|
1247 | {
|
---|
1248 | pte_t pte = *ptep;
|
---|
1249 | pte.pte_high &= 0xfff00000;
|
---|
1250 | pte.pte_high |= ((Phys >> 32) & 0x000fffff);
|
---|
1251 | pte.pte_low &= 0x00000fff;
|
---|
1252 | pte.pte_low |= (Phys & 0xfffff000);
|
---|
1253 | set_pte(ptep, pte);
|
---|
1254 | pte_unmap(ptep);
|
---|
1255 | rc = 0;
|
---|
1256 | }
|
---|
1257 | }
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | spin_unlock(&mm->page_table_lock);
|
---|
1261 | return rc;
|
---|
1262 | }
|
---|
1263 | #endif /* VBOX_USE_PAE_HACK */
|
---|
1264 |
|
---|
1265 |
|
---|
1266 | int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
|
---|
1267 | {
|
---|
1268 | struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
|
---|
1269 | PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
|
---|
1270 | int rc = VERR_NO_MEMORY;
|
---|
1271 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1272 | #ifdef VBOX_USE_PAE_HACK
|
---|
1273 | struct page *pDummyPage;
|
---|
1274 | RTHCPHYS DummyPhys;
|
---|
1275 | #endif
|
---|
1276 |
|
---|
1277 | /*
|
---|
1278 | * Check for restrictions.
|
---|
1279 | */
|
---|
1280 | if (!pTask)
|
---|
1281 | return VERR_NOT_SUPPORTED;
|
---|
1282 | if (uAlignment > PAGE_SIZE)
|
---|
1283 | return VERR_NOT_SUPPORTED;
|
---|
1284 |
|
---|
1285 | #ifdef VBOX_USE_PAE_HACK
|
---|
1286 | /*
|
---|
1287 | * Allocate a dummy page for use when mapping the memory.
|
---|
1288 | */
|
---|
1289 | pDummyPage = alloc_page(GFP_USER);
|
---|
1290 | if (!pDummyPage)
|
---|
1291 | return VERR_NO_MEMORY;
|
---|
1292 | SetPageReserved(pDummyPage);
|
---|
1293 | DummyPhys = page_to_phys(pDummyPage);
|
---|
1294 | #endif
|
---|
1295 |
|
---|
1296 | /*
|
---|
1297 | * Create the IPRT memory object.
|
---|
1298 | */
|
---|
1299 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
|
---|
1300 | if (pMemLnx)
|
---|
1301 | {
|
---|
1302 | /*
|
---|
1303 | * Allocate user space mapping.
|
---|
1304 | */
|
---|
1305 | void *pv;
|
---|
1306 | down_write(&pTask->mm->mmap_sem);
|
---|
1307 | pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, pMemLnxToMap->Core.cb, uAlignment, pTask, fProt);
|
---|
1308 | if (pv != (void *)-1)
|
---|
1309 | {
|
---|
1310 | /*
|
---|
1311 | * Map page by page into the mmap area.
|
---|
1312 | * This is generic, paranoid and not very efficient.
|
---|
1313 | */
|
---|
1314 | pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, false /* user */);
|
---|
1315 | unsigned long ulAddrCur = (unsigned long)pv;
|
---|
1316 | const size_t cPages = pMemLnxToMap->Core.cb >> PAGE_SHIFT;
|
---|
1317 | size_t iPage;
|
---|
1318 |
|
---|
1319 | rc = 0;
|
---|
1320 | if (pMemLnxToMap->cPages)
|
---|
1321 | {
|
---|
1322 | for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
|
---|
1323 | {
|
---|
1324 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
|
---|
1325 | RTHCPHYS Phys = page_to_phys(pMemLnxToMap->apPages[iPage]);
|
---|
1326 | #endif
|
---|
1327 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1328 | struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
|
---|
1329 | AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
|
---|
1330 | #endif
|
---|
1331 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_X86)
|
---|
1332 | /* remap_page_range() limitation on x86 */
|
---|
1333 | AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
|
---|
1334 | #endif
|
---|
1335 |
|
---|
1336 | #if defined(VBOX_USE_INSERT_PAGE) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
|
---|
1337 | rc = vm_insert_page(vma, ulAddrCur, pMemLnxToMap->apPages[iPage]);
|
---|
1338 | vma->vm_flags |= VM_RESERVED; /* This flag helps making 100% sure some bad stuff wont happen (swap, core, ++). */
|
---|
1339 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
|
---|
1340 | rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
|
---|
1341 | #elif defined(VBOX_USE_PAE_HACK)
|
---|
1342 | rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
|
---|
1343 | if (!rc)
|
---|
1344 | rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
|
---|
1345 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1346 | rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1347 | #else /* 2.4 */
|
---|
1348 | rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1349 | #endif
|
---|
1350 | if (rc)
|
---|
1351 | {
|
---|
1352 | rc = VERR_NO_MEMORY;
|
---|
1353 | break;
|
---|
1354 | }
|
---|
1355 | }
|
---|
1356 | }
|
---|
1357 | else
|
---|
1358 | {
|
---|
1359 | RTHCPHYS Phys;
|
---|
1360 | if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS)
|
---|
1361 | Phys = pMemLnxToMap->Core.u.Phys.PhysBase;
|
---|
1362 | else if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_CONT)
|
---|
1363 | Phys = pMemLnxToMap->Core.u.Cont.Phys;
|
---|
1364 | else
|
---|
1365 | {
|
---|
1366 | AssertMsgFailed(("%d\n", pMemLnxToMap->Core.enmType));
|
---|
1367 | Phys = NIL_RTHCPHYS;
|
---|
1368 | }
|
---|
1369 | if (Phys != NIL_RTHCPHYS)
|
---|
1370 | {
|
---|
1371 | for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE, Phys += PAGE_SIZE)
|
---|
1372 | {
|
---|
1373 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1374 | struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
|
---|
1375 | AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
|
---|
1376 | #endif
|
---|
1377 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_X86)
|
---|
1378 | /* remap_page_range() limitation on x86 */
|
---|
1379 | AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
|
---|
1380 | #endif
|
---|
1381 |
|
---|
1382 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
|
---|
1383 | rc = remap_pfn_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1384 | #elif defined(VBOX_USE_PAE_HACK)
|
---|
1385 | rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
|
---|
1386 | if (!rc)
|
---|
1387 | rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
|
---|
1388 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1389 | rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1390 | #else /* 2.4 */
|
---|
1391 | rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1392 | #endif
|
---|
1393 | if (rc)
|
---|
1394 | {
|
---|
1395 | rc = VERR_NO_MEMORY;
|
---|
1396 | break;
|
---|
1397 | }
|
---|
1398 | }
|
---|
1399 | }
|
---|
1400 | }
|
---|
1401 | if (!rc)
|
---|
1402 | {
|
---|
1403 | up_write(&pTask->mm->mmap_sem);
|
---|
1404 | #ifdef VBOX_USE_PAE_HACK
|
---|
1405 | __free_page(pDummyPage);
|
---|
1406 | #endif
|
---|
1407 |
|
---|
1408 | pMemLnx->Core.pv = pv;
|
---|
1409 | pMemLnx->Core.u.Mapping.R0Process = R0Process;
|
---|
1410 | *ppMem = &pMemLnx->Core;
|
---|
1411 | return VINF_SUCCESS;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | /*
|
---|
1415 | * Bail out.
|
---|
1416 | */
|
---|
1417 | MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, pMemLnxToMap->Core.cb);
|
---|
1418 | }
|
---|
1419 | up_write(&pTask->mm->mmap_sem);
|
---|
1420 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
1421 | }
|
---|
1422 | #ifdef VBOX_USE_PAE_HACK
|
---|
1423 | __free_page(pDummyPage);
|
---|
1424 | #endif
|
---|
1425 |
|
---|
1426 | return rc;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 |
|
---|
1430 | int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
|
---|
1431 | {
|
---|
1432 | NOREF(pMem);
|
---|
1433 | NOREF(offSub);
|
---|
1434 | NOREF(cbSub);
|
---|
1435 | NOREF(fProt);
|
---|
1436 | return VERR_NOT_SUPPORTED;
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 |
|
---|
1440 | RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
|
---|
1441 | {
|
---|
1442 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
|
---|
1443 |
|
---|
1444 | if (pMemLnx->cPages)
|
---|
1445 | return page_to_phys(pMemLnx->apPages[iPage]);
|
---|
1446 |
|
---|
1447 | switch (pMemLnx->Core.enmType)
|
---|
1448 | {
|
---|
1449 | case RTR0MEMOBJTYPE_CONT:
|
---|
1450 | return pMemLnx->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
|
---|
1451 |
|
---|
1452 | case RTR0MEMOBJTYPE_PHYS:
|
---|
1453 | return pMemLnx->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
|
---|
1454 |
|
---|
1455 | /* the parent knows */
|
---|
1456 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
1457 | return rtR0MemObjNativeGetPagePhysAddr(pMemLnx->Core.uRel.Child.pParent, iPage);
|
---|
1458 |
|
---|
1459 | /* cPages > 0 */
|
---|
1460 | case RTR0MEMOBJTYPE_LOW:
|
---|
1461 | case RTR0MEMOBJTYPE_LOCK:
|
---|
1462 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
1463 | case RTR0MEMOBJTYPE_PAGE:
|
---|
1464 | default:
|
---|
1465 | AssertMsgFailed(("%d\n", pMemLnx->Core.enmType));
|
---|
1466 | /* fall thru */
|
---|
1467 |
|
---|
1468 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
1469 | return NIL_RTHCPHYS;
|
---|
1470 | }
|
---|
1471 | }
|
---|
1472 |
|
---|