1 | /* $Revision: 6416 $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Ring-0 Memory Objects, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
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/string.h>
|
---|
38 | #include <iprt/process.h>
|
---|
39 | #include "internal/memobj.h"
|
---|
40 |
|
---|
41 | /* early 2.6 kernels */
|
---|
42 | #ifndef PAGE_SHARED_EXEC
|
---|
43 | # define PAGE_SHARED_EXEC PAGE_SHARED
|
---|
44 | #endif
|
---|
45 | #ifndef PAGE_READONLY_EXEC
|
---|
46 | # define PAGE_READONLY_EXEC PAGE_READONLY
|
---|
47 | #endif
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Structures and Typedefs *
|
---|
52 | *******************************************************************************/
|
---|
53 | /**
|
---|
54 | * The Darwin version of the memory object structure.
|
---|
55 | */
|
---|
56 | typedef struct RTR0MEMOBJLNX
|
---|
57 | {
|
---|
58 | /** The core structure. */
|
---|
59 | RTR0MEMOBJINTERNAL Core;
|
---|
60 | /** Set if the allocation is contiguous.
|
---|
61 | * This means it has to be given back as one chunk. */
|
---|
62 | bool fContiguous;
|
---|
63 | /** Set if we've vmap'ed thed memory into ring-0. */
|
---|
64 | bool fMappedToRing0;
|
---|
65 | /** The pages in the apPages array. */
|
---|
66 | size_t cPages;
|
---|
67 | /** Array of struct page pointers. (variable size) */
|
---|
68 | struct page *apPages[1];
|
---|
69 | } RTR0MEMOBJLNX, *PRTR0MEMOBJLNX;
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Helper that converts from a RTR0PROCESS handle to a linux task.
|
---|
74 | *
|
---|
75 | * @returns The corresponding Linux task.
|
---|
76 | * @param R0Process IPRT ring-0 process handle.
|
---|
77 | */
|
---|
78 | struct task_struct *rtR0ProcessToLinuxTask(RTR0PROCESS R0Process)
|
---|
79 | {
|
---|
80 | /** @todo fix rtR0ProcessToLinuxTask!! */
|
---|
81 | return R0Process == RTR0ProcHandleSelf() ? current : NULL;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Compute order. Some functions allocate 2^order pages.
|
---|
87 | *
|
---|
88 | * @returns order.
|
---|
89 | * @param cPages Number of pages.
|
---|
90 | */
|
---|
91 | static int rtR0MemObjLinuxOrder(size_t cPages)
|
---|
92 | {
|
---|
93 | int iOrder;
|
---|
94 | size_t cTmp;
|
---|
95 |
|
---|
96 | for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
|
---|
97 | ;
|
---|
98 | if (cPages & ~((size_t)1 << iOrder))
|
---|
99 | ++iOrder;
|
---|
100 |
|
---|
101 | return iOrder;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Converts from RTMEM_PROT_* to Linux PAGE_*.
|
---|
107 | *
|
---|
108 | * @returns Linux page protection constant.
|
---|
109 | * @param fProt The IPRT protection mask.
|
---|
110 | * @param fKernel Whether it applies to kernel or user space.
|
---|
111 | */
|
---|
112 | static pgprot_t rtR0MemObjLinuxConvertProt(unsigned fProt, bool fKernel)
|
---|
113 | {
|
---|
114 | switch (fProt)
|
---|
115 | {
|
---|
116 | default:
|
---|
117 | AssertMsgFailed(("%#x %d\n", fProt, fKernel));
|
---|
118 | case RTMEM_PROT_NONE:
|
---|
119 | return PAGE_NONE;
|
---|
120 |
|
---|
121 | case RTMEM_PROT_READ:
|
---|
122 | return fKernel ? PAGE_KERNEL_RO : PAGE_READONLY;
|
---|
123 |
|
---|
124 | case RTMEM_PROT_WRITE:
|
---|
125 | case RTMEM_PROT_WRITE | RTMEM_PROT_READ:
|
---|
126 | return fKernel ? PAGE_KERNEL : PAGE_SHARED;
|
---|
127 |
|
---|
128 | case RTMEM_PROT_EXEC:
|
---|
129 | case RTMEM_PROT_EXEC | RTMEM_PROT_READ:
|
---|
130 | #if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
|
---|
131 | if (fKernel)
|
---|
132 | {
|
---|
133 | pgprot_t fPg = MY_PAGE_KERNEL_EXEC;
|
---|
134 | pgprot_val(fPg) &= ~_PAGE_RW;
|
---|
135 | return fPg;
|
---|
136 | }
|
---|
137 | return PAGE_READONLY_EXEC;
|
---|
138 | #else
|
---|
139 | return fKernel ? MY_PAGE_KERNEL_EXEC : PAGE_READONLY_EXEC;
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
|
---|
143 | case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_READ:
|
---|
144 | return fKernel ? MY_PAGE_KERNEL_EXEC : PAGE_SHARED_EXEC;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Internal worker that allocates physical pages and creates the memory object for them.
|
---|
151 | *
|
---|
152 | * @returns IPRT status code.
|
---|
153 | * @param ppMemLnx Where to store the memory object pointer.
|
---|
154 | * @param enmType The object type.
|
---|
155 | * @param cb The number of bytes to allocate.
|
---|
156 | * @param fFlagsLnx The page allocation flags (GPFs).
|
---|
157 | * @param fContiguous Whether the allocation must be contiguous.
|
---|
158 | */
|
---|
159 | static int rtR0MemObjLinuxAllocPages(PRTR0MEMOBJLNX *ppMemLnx, RTR0MEMOBJTYPE enmType, size_t cb, unsigned fFlagsLnx, bool fContiguous)
|
---|
160 | {
|
---|
161 | size_t iPage;
|
---|
162 | size_t cPages = cb >> PAGE_SHIFT;
|
---|
163 | struct page *paPages;
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Allocate a memory object structure that's large enough to contain
|
---|
167 | * the page pointer array.
|
---|
168 | */
|
---|
169 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), enmType, NULL, cb);
|
---|
170 | if (!pMemLnx)
|
---|
171 | return VERR_NO_MEMORY;
|
---|
172 | pMemLnx->cPages = cPages;
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Allocate the pages.
|
---|
176 | * For small allocations we'll try contiguous first and then fall back on page by page.
|
---|
177 | */
|
---|
178 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
179 | if ( fContiguous
|
---|
180 | || cb <= PAGE_SIZE * 2)
|
---|
181 | {
|
---|
182 | paPages = alloc_pages(fFlagsLnx, rtR0MemObjLinuxOrder(cb >> PAGE_SHIFT));
|
---|
183 | if (paPages)
|
---|
184 | {
|
---|
185 | fContiguous = true;
|
---|
186 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
187 | pMemLnx->apPages[iPage] = &paPages[iPage];
|
---|
188 | }
|
---|
189 | else if (fContiguous)
|
---|
190 | {
|
---|
191 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
192 | return VERR_NO_MEMORY;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (!fContiguous)
|
---|
197 | {
|
---|
198 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
199 | {
|
---|
200 | pMemLnx->apPages[iPage] = alloc_page(fFlagsLnx);
|
---|
201 | if (RT_UNLIKELY(!pMemLnx->apPages[iPage]))
|
---|
202 | {
|
---|
203 | while (iPage-- > 0)
|
---|
204 | __free_page(pMemLnx->apPages[iPage]);
|
---|
205 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
206 | return VERR_NO_MEMORY;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | #else /* < 2.4.22 */
|
---|
212 | /** @todo figure out why we didn't allocate page-by-page on 2.4.21 and older... */
|
---|
213 | paPages = alloc_pages(fFlagsLnx, rtR0MemObjLinuxOrder(cb >> PAGE_SHIFT));
|
---|
214 | if (!paPages)
|
---|
215 | {
|
---|
216 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
217 | return VERR_NO_MEMORY;
|
---|
218 | }
|
---|
219 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
220 | {
|
---|
221 | pMemLnx->apPages[iPage] = &paPages[iPage];
|
---|
222 | if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
|
---|
223 | MY_CHANGE_PAGE_ATTR(pMemLnx->apPages[iPage], 1, MY_PAGE_KERNEL_EXEC);
|
---|
224 | if (PageHighMem(pMemLnx->apPages[iPage]))
|
---|
225 | BUG();
|
---|
226 | }
|
---|
227 |
|
---|
228 | fContiguous = true;
|
---|
229 | #endif /* < 2.4.22 */
|
---|
230 | pMemLnx->fContiguous = fContiguous;
|
---|
231 |
|
---|
232 | /*
|
---|
233 | * Reserve the pages.
|
---|
234 | */
|
---|
235 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
236 | SetPageReserved(pMemLnx->apPages[iPage]);
|
---|
237 |
|
---|
238 | *ppMemLnx = pMemLnx;
|
---|
239 | return VINF_SUCCESS;
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Frees the physical pages allocated by the rtR0MemObjLinuxAllocPages() call.
|
---|
245 | *
|
---|
246 | * This method does NOT free the object.
|
---|
247 | *
|
---|
248 | * @param pMemLnx The object which physical pages should be freed.
|
---|
249 | */
|
---|
250 | static void rtR0MemObjLinuxFreePages(PRTR0MEMOBJLNX pMemLnx)
|
---|
251 | {
|
---|
252 | size_t iPage = pMemLnx->cPages;
|
---|
253 | if (iPage > 0)
|
---|
254 | {
|
---|
255 | /*
|
---|
256 | * Restore the page flags.
|
---|
257 | */
|
---|
258 | while (iPage-- > 0)
|
---|
259 | {
|
---|
260 | ClearPageReserved(pMemLnx->apPages[iPage]);
|
---|
261 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
262 | #else
|
---|
263 | if (pgprot_val(MY_PAGE_KERNEL_EXEC) != pgprot_val(PAGE_KERNEL))
|
---|
264 | MY_CHANGE_PAGE_ATTR(pMemLnx->apPages[iPage], 1, PAGE_KERNEL);
|
---|
265 | #endif
|
---|
266 | }
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Free the pages.
|
---|
270 | */
|
---|
271 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
272 | if (!pMemLnx->fContiguous)
|
---|
273 | {
|
---|
274 | iPage = pMemLnx->cPages;
|
---|
275 | while (iPage-- > 0)
|
---|
276 | __free_page(pMemLnx->apPages[iPage]);
|
---|
277 | }
|
---|
278 | else
|
---|
279 | #endif
|
---|
280 | __free_pages(pMemLnx->apPages[0], rtR0MemObjLinuxOrder(pMemLnx->cPages));
|
---|
281 |
|
---|
282 | pMemLnx->cPages = 0;
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Maps the allocation into ring-0.
|
---|
289 | *
|
---|
290 | * This will update the RTR0MEMOBJLNX::Core.pv and RTR0MEMOBJ::fMappedToRing0 members.
|
---|
291 | *
|
---|
292 | * Contiguous mappings that isn't in 'high' memory will already be mapped into kernel
|
---|
293 | * space, so we'll use that mapping if possible. If execute access is required, we'll
|
---|
294 | * play safe and do our own mapping.
|
---|
295 | *
|
---|
296 | * @returns IPRT status code.
|
---|
297 | * @param pMemLnx The linux memory object to map.
|
---|
298 | * @param fExecutable Whether execute access is required.
|
---|
299 | */
|
---|
300 | static int rtR0MemObjLinuxVMap(PRTR0MEMOBJLNX pMemLnx, bool fExecutable)
|
---|
301 | {
|
---|
302 | int rc = VINF_SUCCESS;
|
---|
303 |
|
---|
304 | /*
|
---|
305 | * Choose mapping strategy.
|
---|
306 | */
|
---|
307 | bool fMustMap = fExecutable
|
---|
308 | || !pMemLnx->fContiguous;
|
---|
309 | if (!fMustMap)
|
---|
310 | {
|
---|
311 | size_t iPage = pMemLnx->cPages;
|
---|
312 | while (iPage-- > 0)
|
---|
313 | if (PageHighMem(pMemLnx->apPages[iPage]))
|
---|
314 | {
|
---|
315 | fMustMap = true;
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | Assert(!pMemLnx->Core.pv);
|
---|
321 | Assert(!pMemLnx->fMappedToRing0);
|
---|
322 |
|
---|
323 | if (fMustMap)
|
---|
324 | {
|
---|
325 | /*
|
---|
326 | * Use vmap - 2.4.22 and later.
|
---|
327 | */
|
---|
328 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
329 | pgprot_t fPg;
|
---|
330 | pgprot_val(fPg) = _PAGE_PRESENT | _PAGE_RW;
|
---|
331 | # ifdef _PAGE_NX
|
---|
332 | if (!fExecutable)
|
---|
333 | pgprot_val(fPg) |= _PAGE_NX;
|
---|
334 | # endif
|
---|
335 |
|
---|
336 | # ifdef VM_MAP
|
---|
337 | pMemLnx->Core.pv = vmap(&pMemLnx->apPages[0], pMemLnx->cPages, VM_MAP, fPg);
|
---|
338 | # else
|
---|
339 | pMemLnx->Core.pv = vmap(&pMemLnx->apPages[0], pMemLnx->cPages, VM_ALLOC, fPg);
|
---|
340 | # endif
|
---|
341 | if (pMemLnx->Core.pv)
|
---|
342 | pMemLnx->fMappedToRing0 = true;
|
---|
343 | else
|
---|
344 | rc = VERR_MAP_FAILED;
|
---|
345 | #else /* < 2.4.22 */
|
---|
346 | rc = VERR_NOT_SUPPORTED;
|
---|
347 | #endif
|
---|
348 | }
|
---|
349 | else
|
---|
350 | {
|
---|
351 | /*
|
---|
352 | * Use the kernel RAM mapping.
|
---|
353 | */
|
---|
354 | pMemLnx->Core.pv = phys_to_virt(page_to_phys(pMemLnx->apPages[0]));
|
---|
355 | Assert(pMemLnx->Core.pv);
|
---|
356 | }
|
---|
357 |
|
---|
358 | return rc;
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Undos what rtR0MemObjLinuxVMap() did.
|
---|
364 | *
|
---|
365 | * @param pMemLnx The linux memory object.
|
---|
366 | */
|
---|
367 | static void rtR0MemObjLinuxVUnmap(PRTR0MEMOBJLNX pMemLnx)
|
---|
368 | {
|
---|
369 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
370 | if (pMemLnx->fMappedToRing0)
|
---|
371 | {
|
---|
372 | Assert(pMemLnx->Core.pv);
|
---|
373 | vunmap(pMemLnx->Core.pv);
|
---|
374 | pMemLnx->fMappedToRing0 = false;
|
---|
375 | }
|
---|
376 | #else /* < 2.4.22 */
|
---|
377 | Assert(!pMemLnx->fMappedToRing0);
|
---|
378 | #endif
|
---|
379 | pMemLnx->Core.pv = NULL;
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
|
---|
384 | {
|
---|
385 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
|
---|
386 |
|
---|
387 | /*
|
---|
388 | * Release any memory that we've allocated or locked.
|
---|
389 | */
|
---|
390 | switch (pMemLnx->Core.enmType)
|
---|
391 | {
|
---|
392 | case RTR0MEMOBJTYPE_LOW:
|
---|
393 | case RTR0MEMOBJTYPE_PAGE:
|
---|
394 | case RTR0MEMOBJTYPE_CONT:
|
---|
395 | case RTR0MEMOBJTYPE_PHYS:
|
---|
396 | rtR0MemObjLinuxVUnmap(pMemLnx);
|
---|
397 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
398 | break;
|
---|
399 |
|
---|
400 | case RTR0MEMOBJTYPE_LOCK:
|
---|
401 | if (pMemLnx->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
|
---|
402 | {
|
---|
403 | size_t iPage;
|
---|
404 | struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
|
---|
405 | Assert(pTask);
|
---|
406 | if (pTask && pTask->mm)
|
---|
407 | down_read(&pTask->mm->mmap_sem);
|
---|
408 |
|
---|
409 | iPage = pMemLnx->cPages;
|
---|
410 | while (iPage-- > 0)
|
---|
411 | {
|
---|
412 | if (!PageReserved(pMemLnx->apPages[iPage]))
|
---|
413 | SetPageDirty(pMemLnx->apPages[iPage]);
|
---|
414 | page_cache_release(pMemLnx->apPages[iPage]);
|
---|
415 | }
|
---|
416 |
|
---|
417 | if (pTask && pTask->mm)
|
---|
418 | up_read(&pTask->mm->mmap_sem);
|
---|
419 | }
|
---|
420 | else
|
---|
421 | AssertFailed(); /* not implemented for R0 */
|
---|
422 | break;
|
---|
423 |
|
---|
424 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
425 | Assert(pMemLnx->Core.pv);
|
---|
426 | if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
|
---|
427 | {
|
---|
428 | struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
|
---|
429 | Assert(pTask);
|
---|
430 | if (pTask && pTask->mm)
|
---|
431 | {
|
---|
432 | down_write(&pTask->mm->mmap_sem);
|
---|
433 | MY_DO_MUNMAP(pTask->mm, (unsigned long)pMemLnx->Core.pv, pMemLnx->Core.cb);
|
---|
434 | up_write(&pTask->mm->mmap_sem);
|
---|
435 | }
|
---|
436 | }
|
---|
437 | else
|
---|
438 | {
|
---|
439 | vunmap(pMemLnx->Core.pv);
|
---|
440 |
|
---|
441 | Assert(pMemLnx->cPages == 1 && pMemLnx->apPages[0] != NULL);
|
---|
442 | __free_page(pMemLnx->apPages[0]);
|
---|
443 | pMemLnx->apPages[0] = NULL;
|
---|
444 | pMemLnx->cPages = 0;
|
---|
445 | }
|
---|
446 | pMemLnx->Core.pv = NULL;
|
---|
447 | break;
|
---|
448 |
|
---|
449 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
450 | Assert(pMemLnx->cPages == 0); Assert(pMemLnx->Core.pv);
|
---|
451 | if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
|
---|
452 | {
|
---|
453 | struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
|
---|
454 | Assert(pTask);
|
---|
455 | if (pTask && pTask->mm)
|
---|
456 | {
|
---|
457 | down_write(&pTask->mm->mmap_sem);
|
---|
458 | MY_DO_MUNMAP(pTask->mm, (unsigned long)pMemLnx->Core.pv, pMemLnx->Core.cb);
|
---|
459 | up_write(&pTask->mm->mmap_sem);
|
---|
460 | }
|
---|
461 | }
|
---|
462 | else
|
---|
463 | vunmap(pMemLnx->Core.pv);
|
---|
464 | pMemLnx->Core.pv = NULL;
|
---|
465 | break;
|
---|
466 |
|
---|
467 | default:
|
---|
468 | AssertMsgFailed(("enmType=%d\n", pMemLnx->Core.enmType));
|
---|
469 | return VERR_INTERNAL_ERROR;
|
---|
470 | }
|
---|
471 | return VINF_SUCCESS;
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
476 | {
|
---|
477 | PRTR0MEMOBJLNX pMemLnx;
|
---|
478 | int rc;
|
---|
479 |
|
---|
480 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
481 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, GFP_HIGHUSER, false /* non-contiguous */);
|
---|
482 | #else
|
---|
483 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, GFP_USER, false /* non-contiguous */);
|
---|
484 | #endif
|
---|
485 | if (RT_SUCCESS(rc))
|
---|
486 | {
|
---|
487 | rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
|
---|
488 | if (RT_SUCCESS(rc))
|
---|
489 | {
|
---|
490 | *ppMem = &pMemLnx->Core;
|
---|
491 | return rc;
|
---|
492 | }
|
---|
493 |
|
---|
494 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
495 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
496 | }
|
---|
497 |
|
---|
498 | return rc;
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
503 | {
|
---|
504 | PRTR0MEMOBJLNX pMemLnx;
|
---|
505 | int rc;
|
---|
506 |
|
---|
507 | #ifdef RT_ARCH_AMD64
|
---|
508 | # ifdef GFP_DMA32
|
---|
509 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, GFP_DMA32, false /* non-contiguous */);
|
---|
510 | if (RT_FAILURE(rc))
|
---|
511 | # endif
|
---|
512 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, GFP_DMA, false /* non-contiguous */);
|
---|
513 | #else
|
---|
514 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, GFP_USER, false /* non-contiguous */);
|
---|
515 | #endif
|
---|
516 | if (RT_SUCCESS(rc))
|
---|
517 | {
|
---|
518 | rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
|
---|
519 | if (RT_SUCCESS(rc))
|
---|
520 | {
|
---|
521 | *ppMem = &pMemLnx->Core;
|
---|
522 | return rc;
|
---|
523 | }
|
---|
524 |
|
---|
525 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
526 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
527 | }
|
---|
528 |
|
---|
529 | return rc;
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
534 | {
|
---|
535 | PRTR0MEMOBJLNX pMemLnx;
|
---|
536 | int rc;
|
---|
537 |
|
---|
538 | #ifdef RT_ARCH_AMD64
|
---|
539 | # ifdef GFP_DMA32
|
---|
540 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, GFP_DMA32, true /* contiguous */);
|
---|
541 | if (RT_FAILURE(rc))
|
---|
542 | # endif
|
---|
543 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, GFP_DMA, true /* contiguous */);
|
---|
544 | #else
|
---|
545 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, GFP_USER, true /* contiguous */);
|
---|
546 | #endif
|
---|
547 | if (RT_SUCCESS(rc))
|
---|
548 | {
|
---|
549 | rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
|
---|
550 | if (RT_SUCCESS(rc))
|
---|
551 | {
|
---|
552 | #ifdef RT_STRICT
|
---|
553 | size_t iPage = pMemLnx->cPages;
|
---|
554 | while (iPage-- > 0)
|
---|
555 | Assert(page_to_phys(pMemLnx->apPages[iPage]) < _4G);
|
---|
556 | #endif
|
---|
557 | pMemLnx->Core.u.Cont.Phys = page_to_phys(pMemLnx->apPages[0]);
|
---|
558 | *ppMem = &pMemLnx->Core;
|
---|
559 | return rc;
|
---|
560 | }
|
---|
561 |
|
---|
562 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
563 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
564 | }
|
---|
565 |
|
---|
566 | return rc;
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Worker for rtR0MemObjLinuxAllocPhysSub that tries one allocation strategy.
|
---|
572 | *
|
---|
573 | * @returns IPRT status.
|
---|
574 | * @param ppMemLnx Where to
|
---|
575 | * @param enmType The object type.
|
---|
576 | * @param cb The size of the allocation.
|
---|
577 | * @param PhysHighest See rtR0MemObjNativeAllocPhys.
|
---|
578 | * @param fGfp The Linux GFP flags to use for the allocation.
|
---|
579 | */
|
---|
580 | static int rtR0MemObjLinuxAllocPhysSub2(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType, size_t cb, RTHCPHYS PhysHighest, unsigned fGfp)
|
---|
581 | {
|
---|
582 | PRTR0MEMOBJLNX pMemLnx;
|
---|
583 | int rc;
|
---|
584 |
|
---|
585 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, enmType, cb, fGfp,
|
---|
586 | enmType == RTR0MEMOBJTYPE_PHYS /* contiguous / non-contiguous */);
|
---|
587 | if (RT_FAILURE(rc))
|
---|
588 | return rc;
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * Check the addresses if necessary. (Can be optimized a bit for PHYS.)
|
---|
592 | */
|
---|
593 | if (PhysHighest != NIL_RTHCPHYS)
|
---|
594 | {
|
---|
595 | size_t iPage = pMemLnx->cPages;
|
---|
596 | while (iPage-- > 0)
|
---|
597 | if (page_to_phys(pMemLnx->apPages[iPage]) >= PhysHighest)
|
---|
598 | {
|
---|
599 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
600 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
601 | return VERR_NO_MEMORY;
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * Complete the object.
|
---|
607 | */
|
---|
608 | if (enmType == RTR0MEMOBJTYPE_PHYS)
|
---|
609 | {
|
---|
610 | pMemLnx->Core.u.Phys.PhysBase = page_to_phys(pMemLnx->apPages[0]);
|
---|
611 | pMemLnx->Core.u.Phys.fAllocated = true;
|
---|
612 | }
|
---|
613 | *ppMem = &pMemLnx->Core;
|
---|
614 | return rc;
|
---|
615 | }
|
---|
616 |
|
---|
617 |
|
---|
618 | /**
|
---|
619 | * Worker for rtR0MemObjNativeAllocPhys and rtR0MemObjNativeAllocPhysNC.
|
---|
620 | *
|
---|
621 | * @returns IPRT status.
|
---|
622 | * @param ppMem Where to store the memory object pointer on success.
|
---|
623 | * @param enmType The object type.
|
---|
624 | * @param cb The size of the allocation.
|
---|
625 | * @param PhysHighest See rtR0MemObjNativeAllocPhys.
|
---|
626 | */
|
---|
627 | static int rtR0MemObjLinuxAllocPhysSub(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType, size_t cb, RTHCPHYS PhysHighest)
|
---|
628 | {
|
---|
629 | int rc;
|
---|
630 |
|
---|
631 | /*
|
---|
632 | * There are two clear cases and that's the <=16MB and anything-goes ones.
|
---|
633 | * When the physical address limit is somewhere inbetween those two we'll
|
---|
634 | * just have to try, starting with HIGHUSER and working our way thru the
|
---|
635 | * different types, hoping we'll get lucky.
|
---|
636 | *
|
---|
637 | * We should probably move this physical address restriction logic up to
|
---|
638 | * the page alloc function as it would be more efficient there. But since
|
---|
639 | * we don't expect this to be a performance issue just yet it can wait.
|
---|
640 | */
|
---|
641 | if (PhysHighest == NIL_RTHCPHYS)
|
---|
642 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_HIGHUSER);
|
---|
643 | else if (PhysHighest <= _1M * 16)
|
---|
644 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_DMA);
|
---|
645 | else
|
---|
646 | {
|
---|
647 | rc = VERR_NO_MEMORY;
|
---|
648 | if (RT_FAILURE(rc))
|
---|
649 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_HIGHUSER);
|
---|
650 | if (RT_FAILURE(rc))
|
---|
651 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_USER);
|
---|
652 | #ifdef GFP_DMA32
|
---|
653 | if (RT_FAILURE(rc))
|
---|
654 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_DMA32);
|
---|
655 | #endif
|
---|
656 | if (RT_FAILURE(rc))
|
---|
657 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, PhysHighest, GFP_DMA);
|
---|
658 | }
|
---|
659 | return rc;
|
---|
660 | }
|
---|
661 |
|
---|
662 |
|
---|
663 | int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
664 | {
|
---|
665 | return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS, cb, PhysHighest);
|
---|
666 | }
|
---|
667 |
|
---|
668 |
|
---|
669 | int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
670 | {
|
---|
671 | return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS_NC, cb, PhysHighest);
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
|
---|
676 | {
|
---|
677 | /*
|
---|
678 | * All we need to do here is to validate that we can use
|
---|
679 | * ioremap on the specified address (32/64-bit dma_addr_t).
|
---|
680 | */
|
---|
681 | PRTR0MEMOBJLNX pMemLnx;
|
---|
682 | dma_addr_t PhysAddr = Phys;
|
---|
683 | AssertMsgReturn(PhysAddr == Phys, ("%#llx\n", (unsigned long long)Phys), VERR_ADDRESS_TOO_BIG);
|
---|
684 |
|
---|
685 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
686 | if (!pMemLnx)
|
---|
687 | return VERR_NO_MEMORY;
|
---|
688 |
|
---|
689 | pMemLnx->Core.u.Phys.PhysBase = PhysAddr;
|
---|
690 | pMemLnx->Core.u.Phys.fAllocated = false;
|
---|
691 | Assert(!pMemLnx->cPages);
|
---|
692 | *ppMem = &pMemLnx->Core;
|
---|
693 | return VINF_SUCCESS;
|
---|
694 | }
|
---|
695 |
|
---|
696 |
|
---|
697 | int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
|
---|
698 | {
|
---|
699 | const int cPages = cb >> PAGE_SHIFT;
|
---|
700 | struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
|
---|
701 | struct vm_area_struct **papVMAs;
|
---|
702 | PRTR0MEMOBJLNX pMemLnx;
|
---|
703 | int rc = VERR_NO_MEMORY;
|
---|
704 |
|
---|
705 | /*
|
---|
706 | * Check for valid task and size overflows.
|
---|
707 | */
|
---|
708 | if (!pTask)
|
---|
709 | return VERR_NOT_SUPPORTED;
|
---|
710 | if (((size_t)cPages << PAGE_SHIFT) != cb)
|
---|
711 | return VERR_OUT_OF_RANGE;
|
---|
712 |
|
---|
713 | /*
|
---|
714 | * Allocate the memory object and a temporary buffer for the VMAs.
|
---|
715 | */
|
---|
716 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
|
---|
717 | if (!pMemLnx)
|
---|
718 | return VERR_NO_MEMORY;
|
---|
719 |
|
---|
720 | papVMAs = (struct vm_area_struct **)RTMemAlloc(sizeof(*papVMAs) * cPages);
|
---|
721 | if (papVMAs)
|
---|
722 | {
|
---|
723 | down_read(&pTask->mm->mmap_sem);
|
---|
724 |
|
---|
725 | /*
|
---|
726 | * Get user pages.
|
---|
727 | */
|
---|
728 | rc = get_user_pages(pTask, /* Task for fault acounting. */
|
---|
729 | pTask->mm, /* Whose pages. */
|
---|
730 | R3Ptr, /* Where from. */
|
---|
731 | cPages, /* How many pages. */
|
---|
732 | 1, /* Write to memory. */
|
---|
733 | 0, /* force. */
|
---|
734 | &pMemLnx->apPages[0], /* Page array. */
|
---|
735 | papVMAs); /* vmas */
|
---|
736 | if (rc == cPages)
|
---|
737 | {
|
---|
738 | /*
|
---|
739 | * Flush dcache (required?) and protect against fork.
|
---|
740 | */
|
---|
741 | /** @todo The Linux fork() protection will require more work if this API
|
---|
742 | * is to be used for anything but locking VM pages. */
|
---|
743 | while (rc-- > 0)
|
---|
744 | {
|
---|
745 | flush_dcache_page(pMemLnx->apPages[rc]);
|
---|
746 | papVMAs[rc]->vm_flags |= VM_DONTCOPY;
|
---|
747 | }
|
---|
748 |
|
---|
749 | up_read(&pTask->mm->mmap_sem);
|
---|
750 |
|
---|
751 | RTMemFree(papVMAs);
|
---|
752 |
|
---|
753 | pMemLnx->Core.u.Lock.R0Process = R0Process;
|
---|
754 | pMemLnx->cPages = cPages;
|
---|
755 | Assert(!pMemLnx->fMappedToRing0);
|
---|
756 | *ppMem = &pMemLnx->Core;
|
---|
757 |
|
---|
758 | return VINF_SUCCESS;
|
---|
759 | }
|
---|
760 |
|
---|
761 | /*
|
---|
762 | * Failed - we need to unlock any pages that we succeeded to lock.
|
---|
763 | */
|
---|
764 | while (rc-- > 0)
|
---|
765 | {
|
---|
766 | if (!PageReserved(pMemLnx->apPages[rc]))
|
---|
767 | SetPageDirty(pMemLnx->apPages[rc]);
|
---|
768 | page_cache_release(pMemLnx->apPages[rc]);
|
---|
769 | }
|
---|
770 |
|
---|
771 | up_read(&pTask->mm->mmap_sem);
|
---|
772 |
|
---|
773 | RTMemFree(papVMAs);
|
---|
774 | rc = VERR_LOCK_FAILED;
|
---|
775 | }
|
---|
776 |
|
---|
777 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
778 | return rc;
|
---|
779 | }
|
---|
780 |
|
---|
781 |
|
---|
782 | int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
|
---|
783 | {
|
---|
784 | /* What is there to lock? Should/Can we fake this? */
|
---|
785 | return VERR_NOT_SUPPORTED;
|
---|
786 | }
|
---|
787 |
|
---|
788 |
|
---|
789 | int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
|
---|
790 | {
|
---|
791 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
792 | const size_t cPages = cb >> PAGE_SHIFT;
|
---|
793 | struct page *pDummyPage;
|
---|
794 | struct page **papPages;
|
---|
795 |
|
---|
796 | /* check for unsupported stuff. */
|
---|
797 | AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
|
---|
798 | AssertMsgReturn(uAlignment <= PAGE_SIZE, ("%#x\n", uAlignment), VERR_NOT_SUPPORTED);
|
---|
799 |
|
---|
800 | /*
|
---|
801 | * Allocate a dummy page and create a page pointer array for vmap such that
|
---|
802 | * the dummy page is mapped all over the reserved area.
|
---|
803 | */
|
---|
804 | pDummyPage = alloc_page(GFP_HIGHUSER);
|
---|
805 | if (!pDummyPage)
|
---|
806 | return VERR_NO_MEMORY;
|
---|
807 | papPages = RTMemAlloc(sizeof(*papPages) * cPages);
|
---|
808 | if (papPages)
|
---|
809 | {
|
---|
810 | void *pv;
|
---|
811 | size_t iPage = cPages;
|
---|
812 | while (iPage-- > 0)
|
---|
813 | papPages[iPage] = pDummyPage;
|
---|
814 | # ifdef VM_MAP
|
---|
815 | pv = vmap(papPages, cPages, VM_MAP, PAGE_KERNEL_RO);
|
---|
816 | # else
|
---|
817 | pv = vmap(papPages, cPages, VM_ALLOC, PAGE_KERNEL_RO);
|
---|
818 | # endif
|
---|
819 | RTMemFree(papPages);
|
---|
820 | if (pv)
|
---|
821 | {
|
---|
822 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
|
---|
823 | if (pMemLnx)
|
---|
824 | {
|
---|
825 | pMemLnx->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
|
---|
826 | pMemLnx->cPages = 1;
|
---|
827 | pMemLnx->apPages[0] = pDummyPage;
|
---|
828 | *ppMem = &pMemLnx->Core;
|
---|
829 | return VINF_SUCCESS;
|
---|
830 | }
|
---|
831 | vunmap(pv);
|
---|
832 | }
|
---|
833 | }
|
---|
834 | __free_page(pDummyPage);
|
---|
835 | return VERR_NO_MEMORY;
|
---|
836 |
|
---|
837 | #else /* < 2.4.22 */
|
---|
838 | /*
|
---|
839 | * Could probably use ioremap here, but the caller is in a better position than us
|
---|
840 | * to select some safe physical memory.
|
---|
841 | */
|
---|
842 | return VERR_NOT_SUPPORTED;
|
---|
843 | #endif
|
---|
844 | }
|
---|
845 |
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Worker for rtR0MemObjNativeReserveUser and rtR0MemObjNativerMapUser that creates
|
---|
849 | * an empty user space mapping.
|
---|
850 | *
|
---|
851 | * The caller takes care of acquiring the mmap_sem of the task.
|
---|
852 | *
|
---|
853 | * @returns Pointer to the mapping.
|
---|
854 | * (void *)-1 on failure.
|
---|
855 | * @param R3PtrFixed (RTR3PTR)-1 if anywhere, otherwise a specific location.
|
---|
856 | * @param cb The size of the mapping.
|
---|
857 | * @param uAlignment The alignment of the mapping.
|
---|
858 | * @param pTask The Linux task to create this mapping in.
|
---|
859 | * @param fProt The RTMEM_PROT_* mask.
|
---|
860 | */
|
---|
861 | static void *rtR0MemObjLinuxDoMmap(RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, struct task_struct *pTask, unsigned fProt)
|
---|
862 | {
|
---|
863 | unsigned fLnxProt;
|
---|
864 | unsigned long ulAddr;
|
---|
865 |
|
---|
866 | /*
|
---|
867 | * Convert from IPRT protection to mman.h PROT_ and call do_mmap.
|
---|
868 | */
|
---|
869 | fProt &= (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
|
---|
870 | if (fProt == RTMEM_PROT_NONE)
|
---|
871 | fLnxProt = PROT_NONE;
|
---|
872 | else
|
---|
873 | {
|
---|
874 | fLnxProt = 0;
|
---|
875 | if (fProt & RTMEM_PROT_READ)
|
---|
876 | fLnxProt |= PROT_READ;
|
---|
877 | if (fProt & RTMEM_PROT_WRITE)
|
---|
878 | fLnxProt |= PROT_WRITE;
|
---|
879 | if (fProt & RTMEM_PROT_EXEC)
|
---|
880 | fLnxProt |= PROT_EXEC;
|
---|
881 | }
|
---|
882 |
|
---|
883 | if (R3PtrFixed != (RTR3PTR)-1)
|
---|
884 | ulAddr = do_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
|
---|
885 | else
|
---|
886 | {
|
---|
887 | ulAddr = do_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
|
---|
888 | if ( !(ulAddr & ~PAGE_MASK)
|
---|
889 | && (ulAddr & (uAlignment - 1)))
|
---|
890 | {
|
---|
891 | /** @todo implement uAlignment properly... We'll probably need to make some dummy mappings to fill
|
---|
892 | * up alignment gaps. This is of course complicated by fragmentation (which we might have cause
|
---|
893 | * ourselves) and further by there begin two mmap strategies (top / bottom). */
|
---|
894 | /* For now, just ignore uAlignment requirements... */
|
---|
895 | }
|
---|
896 | }
|
---|
897 | if (ulAddr & ~PAGE_MASK) /* ~PAGE_MASK == PAGE_OFFSET_MASK */
|
---|
898 | return (void *)-1;
|
---|
899 | return (void *)ulAddr;
|
---|
900 | }
|
---|
901 |
|
---|
902 |
|
---|
903 | int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
|
---|
904 | {
|
---|
905 | PRTR0MEMOBJLNX pMemLnx;
|
---|
906 | void *pv;
|
---|
907 | struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
|
---|
908 | if (!pTask)
|
---|
909 | return VERR_NOT_SUPPORTED;
|
---|
910 |
|
---|
911 | /*
|
---|
912 | * Let rtR0MemObjLinuxDoMmap do the difficult bits.
|
---|
913 | */
|
---|
914 | down_write(&pTask->mm->mmap_sem);
|
---|
915 | pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cb, uAlignment, pTask, RTMEM_PROT_NONE);
|
---|
916 | up_write(&pTask->mm->mmap_sem);
|
---|
917 | if (pv == (void *)-1)
|
---|
918 | return VERR_NO_MEMORY;
|
---|
919 |
|
---|
920 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
|
---|
921 | if (!pMemLnx)
|
---|
922 | {
|
---|
923 | down_write(&pTask->mm->mmap_sem);
|
---|
924 | MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, cb);
|
---|
925 | up_write(&pTask->mm->mmap_sem);
|
---|
926 | return VERR_NO_MEMORY;
|
---|
927 | }
|
---|
928 |
|
---|
929 | pMemLnx->Core.u.ResVirt.R0Process = R0Process;
|
---|
930 | *ppMem = &pMemLnx->Core;
|
---|
931 | return VINF_SUCCESS;
|
---|
932 | }
|
---|
933 |
|
---|
934 |
|
---|
935 | int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
|
---|
936 | {
|
---|
937 | int rc = VERR_NO_MEMORY;
|
---|
938 | PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
|
---|
939 | PRTR0MEMOBJLNX pMemLnx;
|
---|
940 |
|
---|
941 | /* Fail if requested to do something we can't. */
|
---|
942 | AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
|
---|
943 | AssertMsgReturn(uAlignment <= PAGE_SIZE, ("%#x\n", uAlignment), VERR_NOT_SUPPORTED);
|
---|
944 |
|
---|
945 | /*
|
---|
946 | * Create the IPRT memory object.
|
---|
947 | */
|
---|
948 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
|
---|
949 | if (pMemLnx)
|
---|
950 | {
|
---|
951 | if (pMemLnxToMap->cPages)
|
---|
952 | {
|
---|
953 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
954 | /*
|
---|
955 | * Use vmap - 2.4.22 and later.
|
---|
956 | */
|
---|
957 | pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, true /* kernel */);
|
---|
958 | # ifdef VM_MAP
|
---|
959 | pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_MAP, fPg);
|
---|
960 | # else
|
---|
961 | pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_ALLOC, fPg);
|
---|
962 | # endif
|
---|
963 | if (pMemLnx->Core.pv)
|
---|
964 | {
|
---|
965 | pMemLnx->fMappedToRing0 = true;
|
---|
966 | rc = VINF_SUCCESS;
|
---|
967 | }
|
---|
968 | else
|
---|
969 | rc = VERR_MAP_FAILED;
|
---|
970 |
|
---|
971 | #else /* < 2.4.22 */
|
---|
972 | /*
|
---|
973 | * Only option here is to share mappings if possible and forget about fProt.
|
---|
974 | */
|
---|
975 | if (rtR0MemObjIsRing3(pMemToMap))
|
---|
976 | rc = VERR_NOT_SUPPORTED;
|
---|
977 | else
|
---|
978 | {
|
---|
979 | rc = VINF_SUCCESS;
|
---|
980 | if (!pMemLnxToMap->Core.pv)
|
---|
981 | rc = rtR0MemObjLinuxVMap(pMemLnxToMap, !!(fProt & RTMEM_PROT_EXEC));
|
---|
982 | if (RT_SUCCESS(rc))
|
---|
983 | {
|
---|
984 | Assert(pMemLnxToMap->Core.pv);
|
---|
985 | pMemLnx->Core.pv = pMemLnxToMap->Core.pv;
|
---|
986 | }
|
---|
987 | }
|
---|
988 | #endif
|
---|
989 | }
|
---|
990 | else
|
---|
991 | {
|
---|
992 | /*
|
---|
993 | * MMIO / physical memory.
|
---|
994 | */
|
---|
995 | Assert(pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS && !pMemLnxToMap->Core.u.Phys.fAllocated);
|
---|
996 | pMemLnx->Core.pv = ioremap(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb);
|
---|
997 | if (pMemLnx->Core.pv)
|
---|
998 | {
|
---|
999 | /** @todo fix protection. */
|
---|
1000 | rc = VINF_SUCCESS;
|
---|
1001 | }
|
---|
1002 | }
|
---|
1003 | if (RT_SUCCESS(rc))
|
---|
1004 | {
|
---|
1005 | pMemLnx->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
|
---|
1006 | *ppMem = &pMemLnx->Core;
|
---|
1007 | return VINF_SUCCESS;
|
---|
1008 | }
|
---|
1009 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | return rc;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 |
|
---|
1016 | int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
|
---|
1017 | {
|
---|
1018 | struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
|
---|
1019 | PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
|
---|
1020 | int rc = VERR_NO_MEMORY;
|
---|
1021 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1022 |
|
---|
1023 | /*
|
---|
1024 | * Check for restrictions.
|
---|
1025 | */
|
---|
1026 | if (!pTask)
|
---|
1027 | return VERR_NOT_SUPPORTED;
|
---|
1028 |
|
---|
1029 | /*
|
---|
1030 | * Create the IPRT memory object.
|
---|
1031 | */
|
---|
1032 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
|
---|
1033 | if (pMemLnx)
|
---|
1034 | {
|
---|
1035 | /*
|
---|
1036 | * Allocate user space mapping.
|
---|
1037 | */
|
---|
1038 | void *pv;
|
---|
1039 | down_write(&pTask->mm->mmap_sem);
|
---|
1040 | pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, pMemLnxToMap->Core.cb, uAlignment, pTask, fProt);
|
---|
1041 | if (pv != (void *)-1)
|
---|
1042 | {
|
---|
1043 | /*
|
---|
1044 | * Map page by page into the mmap area.
|
---|
1045 | * This is generic, paranoid and not very efficient.
|
---|
1046 | */
|
---|
1047 | pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, false /* user */);
|
---|
1048 | unsigned long ulAddrCur = (unsigned long)pv;
|
---|
1049 | const size_t cPages = pMemLnxToMap->Core.cb >> PAGE_SHIFT;
|
---|
1050 | size_t iPage;
|
---|
1051 | rc = 0;
|
---|
1052 | if (pMemLnxToMap->cPages)
|
---|
1053 | {
|
---|
1054 | for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
|
---|
1055 | {
|
---|
1056 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1057 | struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
|
---|
1058 | AssertBreak(vma, rc = VERR_INTERNAL_ERROR);
|
---|
1059 | #endif
|
---|
1060 |
|
---|
1061 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
|
---|
1062 | rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
|
---|
1063 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1064 | rc = remap_page_range(vma, ulAddrCur, page_to_phys(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
|
---|
1065 | #else /* 2.4 */
|
---|
1066 | rc = remap_page_range(ulAddrCur, page_to_phys(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
|
---|
1067 | #endif
|
---|
1068 | if (rc)
|
---|
1069 | break;
|
---|
1070 | }
|
---|
1071 | }
|
---|
1072 | else
|
---|
1073 | {
|
---|
1074 | RTHCPHYS Phys;
|
---|
1075 | if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS)
|
---|
1076 | Phys = pMemLnxToMap->Core.u.Phys.PhysBase;
|
---|
1077 | else if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_CONT)
|
---|
1078 | Phys = pMemLnxToMap->Core.u.Cont.Phys;
|
---|
1079 | else
|
---|
1080 | {
|
---|
1081 | AssertMsgFailed(("%d\n", pMemLnxToMap->Core.enmType));
|
---|
1082 | Phys = NIL_RTHCPHYS;
|
---|
1083 | }
|
---|
1084 | if (Phys != NIL_RTHCPHYS)
|
---|
1085 | {
|
---|
1086 | for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE, Phys += PAGE_SIZE)
|
---|
1087 | {
|
---|
1088 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1089 | struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
|
---|
1090 | AssertBreak(vma, rc = VERR_INTERNAL_ERROR);
|
---|
1091 | #endif
|
---|
1092 |
|
---|
1093 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
|
---|
1094 | rc = remap_pfn_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1095 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1096 | rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1097 | #else /* 2.4 */
|
---|
1098 | rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1099 | #endif
|
---|
1100 | if (rc)
|
---|
1101 | break;
|
---|
1102 | }
|
---|
1103 | }
|
---|
1104 | }
|
---|
1105 | if (!rc)
|
---|
1106 | {
|
---|
1107 | up_write(&pTask->mm->mmap_sem);
|
---|
1108 |
|
---|
1109 | pMemLnx->Core.pv = pv;
|
---|
1110 | pMemLnx->Core.u.Mapping.R0Process = R0Process;
|
---|
1111 | *ppMem = &pMemLnx->Core;
|
---|
1112 | return VINF_SUCCESS;
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | /*
|
---|
1116 | * Bail out.
|
---|
1117 | */
|
---|
1118 | MY_DO_MUNMAP(pTask->mm, (unsigned long)pv, pMemLnxToMap->Core.cb);
|
---|
1119 | if (rc != VERR_INTERNAL_ERROR)
|
---|
1120 | rc = VERR_NO_MEMORY;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | up_write(&pTask->mm->mmap_sem);
|
---|
1124 |
|
---|
1125 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | return rc;
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 |
|
---|
1132 | RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
|
---|
1133 | {
|
---|
1134 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
|
---|
1135 |
|
---|
1136 | if (pMemLnx->cPages)
|
---|
1137 | return page_to_phys(pMemLnx->apPages[iPage]);
|
---|
1138 |
|
---|
1139 | switch (pMemLnx->Core.enmType)
|
---|
1140 | {
|
---|
1141 | case RTR0MEMOBJTYPE_CONT:
|
---|
1142 | return pMemLnx->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
|
---|
1143 |
|
---|
1144 | case RTR0MEMOBJTYPE_PHYS:
|
---|
1145 | return pMemLnx->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
|
---|
1146 |
|
---|
1147 | /* the parent knows */
|
---|
1148 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
1149 | return rtR0MemObjNativeGetPagePhysAddr(pMemLnx->Core.uRel.Child.pParent, iPage);
|
---|
1150 |
|
---|
1151 | /* cPages > 0 */
|
---|
1152 | case RTR0MEMOBJTYPE_LOW:
|
---|
1153 | case RTR0MEMOBJTYPE_LOCK:
|
---|
1154 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
1155 | case RTR0MEMOBJTYPE_PAGE:
|
---|
1156 | default:
|
---|
1157 | AssertMsgFailed(("%d\n", pMemLnx->Core.enmType));
|
---|
1158 | /* fall thru */
|
---|
1159 |
|
---|
1160 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
1161 | return NIL_RTHCPHYS;
|
---|
1162 | }
|
---|
1163 | }
|
---|
1164 |
|
---|