1 | /* $Id: memobj-r0drv-linux.c 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Ring-0 Memory Objects, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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/assert.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/log.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/process.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include "internal/memobj.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Defined Constants And Macros *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | /* early 2.6 kernels */
|
---|
47 | #ifndef PAGE_SHARED_EXEC
|
---|
48 | # define PAGE_SHARED_EXEC PAGE_SHARED
|
---|
49 | #endif
|
---|
50 | #ifndef PAGE_READONLY_EXEC
|
---|
51 | # define PAGE_READONLY_EXEC PAGE_READONLY
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * 2.6.29+ kernels don't work with remap_pfn_range() anymore because
|
---|
56 | * track_pfn_vma_new() is apparently not defined for non-RAM pages.
|
---|
57 | * It should be safe to use vm_insert_page() older kernels as well.
|
---|
58 | */
|
---|
59 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
|
---|
60 | # define VBOX_USE_INSERT_PAGE
|
---|
61 | #endif
|
---|
62 | #if defined(CONFIG_X86_PAE) \
|
---|
63 | && ( defined(HAVE_26_STYLE_REMAP_PAGE_RANGE) \
|
---|
64 | || ( LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) \
|
---|
65 | && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)))
|
---|
66 | # define VBOX_USE_PAE_HACK
|
---|
67 | #endif
|
---|
68 |
|
---|
69 |
|
---|
70 | /*********************************************************************************************************************************
|
---|
71 | * Structures and Typedefs *
|
---|
72 | *********************************************************************************************************************************/
|
---|
73 | /**
|
---|
74 | * The Darwin version of the memory object structure.
|
---|
75 | */
|
---|
76 | typedef struct RTR0MEMOBJLNX
|
---|
77 | {
|
---|
78 | /** The core structure. */
|
---|
79 | RTR0MEMOBJINTERNAL Core;
|
---|
80 | /** Set if the allocation is contiguous.
|
---|
81 | * This means it has to be given back as one chunk. */
|
---|
82 | bool fContiguous;
|
---|
83 | /** Set if we've vmap'ed the memory into ring-0. */
|
---|
84 | bool fMappedToRing0;
|
---|
85 | /** The pages in the apPages array. */
|
---|
86 | size_t cPages;
|
---|
87 | /** Array of struct page pointers. (variable size) */
|
---|
88 | struct page *apPages[1];
|
---|
89 | } RTR0MEMOBJLNX, *PRTR0MEMOBJLNX;
|
---|
90 |
|
---|
91 |
|
---|
92 | static void rtR0MemObjLinuxFreePages(PRTR0MEMOBJLNX pMemLnx);
|
---|
93 |
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Helper that converts from a RTR0PROCESS handle to a linux task.
|
---|
97 | *
|
---|
98 | * @returns The corresponding Linux task.
|
---|
99 | * @param R0Process IPRT ring-0 process handle.
|
---|
100 | */
|
---|
101 | static struct task_struct *rtR0ProcessToLinuxTask(RTR0PROCESS R0Process)
|
---|
102 | {
|
---|
103 | /** @todo fix rtR0ProcessToLinuxTask!! */
|
---|
104 | /** @todo many (all?) callers currently assume that we return 'current'! */
|
---|
105 | return R0Process == RTR0ProcHandleSelf() ? current : NULL;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Compute order. Some functions allocate 2^order pages.
|
---|
111 | *
|
---|
112 | * @returns order.
|
---|
113 | * @param cPages Number of pages.
|
---|
114 | */
|
---|
115 | static int rtR0MemObjLinuxOrder(size_t cPages)
|
---|
116 | {
|
---|
117 | int iOrder;
|
---|
118 | size_t cTmp;
|
---|
119 |
|
---|
120 | for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
|
---|
121 | ;
|
---|
122 | if (cPages & ~((size_t)1 << iOrder))
|
---|
123 | ++iOrder;
|
---|
124 |
|
---|
125 | return iOrder;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Converts from RTMEM_PROT_* to Linux PAGE_*.
|
---|
131 | *
|
---|
132 | * @returns Linux page protection constant.
|
---|
133 | * @param fProt The IPRT protection mask.
|
---|
134 | * @param fKernel Whether it applies to kernel or user space.
|
---|
135 | */
|
---|
136 | static pgprot_t rtR0MemObjLinuxConvertProt(unsigned fProt, bool fKernel)
|
---|
137 | {
|
---|
138 | switch (fProt)
|
---|
139 | {
|
---|
140 | default:
|
---|
141 | AssertMsgFailed(("%#x %d\n", fProt, fKernel));
|
---|
142 | case RTMEM_PROT_NONE:
|
---|
143 | return PAGE_NONE;
|
---|
144 |
|
---|
145 | case RTMEM_PROT_READ:
|
---|
146 | return fKernel ? PAGE_KERNEL_RO : PAGE_READONLY;
|
---|
147 |
|
---|
148 | case RTMEM_PROT_WRITE:
|
---|
149 | case RTMEM_PROT_WRITE | RTMEM_PROT_READ:
|
---|
150 | return fKernel ? PAGE_KERNEL : PAGE_SHARED;
|
---|
151 |
|
---|
152 | case RTMEM_PROT_EXEC:
|
---|
153 | case RTMEM_PROT_EXEC | RTMEM_PROT_READ:
|
---|
154 | #if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
|
---|
155 | if (fKernel)
|
---|
156 | {
|
---|
157 | pgprot_t fPg = MY_PAGE_KERNEL_EXEC;
|
---|
158 | pgprot_val(fPg) &= ~_PAGE_RW;
|
---|
159 | return fPg;
|
---|
160 | }
|
---|
161 | return PAGE_READONLY_EXEC;
|
---|
162 | #else
|
---|
163 | return fKernel ? MY_PAGE_KERNEL_EXEC : PAGE_READONLY_EXEC;
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
|
---|
167 | case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC | RTMEM_PROT_READ:
|
---|
168 | return fKernel ? MY_PAGE_KERNEL_EXEC : PAGE_SHARED_EXEC;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Worker for rtR0MemObjNativeReserveUser and rtR0MemObjNativerMapUser that creates
|
---|
175 | * an empty user space mapping.
|
---|
176 | *
|
---|
177 | * We acquire the mmap_sem of the task!
|
---|
178 | *
|
---|
179 | * @returns Pointer to the mapping.
|
---|
180 | * (void *)-1 on failure.
|
---|
181 | * @param R3PtrFixed (RTR3PTR)-1 if anywhere, otherwise a specific location.
|
---|
182 | * @param cb The size of the mapping.
|
---|
183 | * @param uAlignment The alignment of the mapping.
|
---|
184 | * @param pTask The Linux task to create this mapping in.
|
---|
185 | * @param fProt The RTMEM_PROT_* mask.
|
---|
186 | */
|
---|
187 | static void *rtR0MemObjLinuxDoMmap(RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, struct task_struct *pTask, unsigned fProt)
|
---|
188 | {
|
---|
189 | unsigned fLnxProt;
|
---|
190 | unsigned long ulAddr;
|
---|
191 |
|
---|
192 | Assert(pTask == current); /* do_mmap */
|
---|
193 | RT_NOREF_PV(pTask);
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * Convert from IPRT protection to mman.h PROT_ and call do_mmap.
|
---|
197 | */
|
---|
198 | fProt &= (RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
|
---|
199 | if (fProt == RTMEM_PROT_NONE)
|
---|
200 | fLnxProt = PROT_NONE;
|
---|
201 | else
|
---|
202 | {
|
---|
203 | fLnxProt = 0;
|
---|
204 | if (fProt & RTMEM_PROT_READ)
|
---|
205 | fLnxProt |= PROT_READ;
|
---|
206 | if (fProt & RTMEM_PROT_WRITE)
|
---|
207 | fLnxProt |= PROT_WRITE;
|
---|
208 | if (fProt & RTMEM_PROT_EXEC)
|
---|
209 | fLnxProt |= PROT_EXEC;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (R3PtrFixed != (RTR3PTR)-1)
|
---|
213 | {
|
---|
214 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
|
---|
215 | ulAddr = vm_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
|
---|
216 | #else
|
---|
217 | down_write(&pTask->mm->mmap_sem);
|
---|
218 | ulAddr = do_mmap(NULL, R3PtrFixed, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, 0);
|
---|
219 | up_write(&pTask->mm->mmap_sem);
|
---|
220 | #endif
|
---|
221 | }
|
---|
222 | else
|
---|
223 | {
|
---|
224 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
|
---|
225 | ulAddr = vm_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
|
---|
226 | #else
|
---|
227 | down_write(&pTask->mm->mmap_sem);
|
---|
228 | ulAddr = do_mmap(NULL, 0, cb, fLnxProt, MAP_SHARED | MAP_ANONYMOUS, 0);
|
---|
229 | up_write(&pTask->mm->mmap_sem);
|
---|
230 | #endif
|
---|
231 | if ( !(ulAddr & ~PAGE_MASK)
|
---|
232 | && (ulAddr & (uAlignment - 1)))
|
---|
233 | {
|
---|
234 | /** @todo implement uAlignment properly... We'll probably need to make some dummy mappings to fill
|
---|
235 | * up alignment gaps. This is of course complicated by fragmentation (which we might have cause
|
---|
236 | * ourselves) and further by there begin two mmap strategies (top / bottom). */
|
---|
237 | /* For now, just ignore uAlignment requirements... */
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | if (ulAddr & ~PAGE_MASK) /* ~PAGE_MASK == PAGE_OFFSET_MASK */
|
---|
243 | return (void *)-1;
|
---|
244 | return (void *)ulAddr;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Worker that destroys a user space mapping.
|
---|
250 | * Undoes what rtR0MemObjLinuxDoMmap did.
|
---|
251 | *
|
---|
252 | * We acquire the mmap_sem of the task!
|
---|
253 | *
|
---|
254 | * @param pv The ring-3 mapping.
|
---|
255 | * @param cb The size of the mapping.
|
---|
256 | * @param pTask The Linux task to destroy this mapping in.
|
---|
257 | */
|
---|
258 | static void rtR0MemObjLinuxDoMunmap(void *pv, size_t cb, struct task_struct *pTask)
|
---|
259 | {
|
---|
260 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
|
---|
261 | Assert(pTask == current); RT_NOREF_PV(pTask);
|
---|
262 | vm_munmap((unsigned long)pv, cb);
|
---|
263 | #elif defined(USE_RHEL4_MUNMAP)
|
---|
264 | down_write(&pTask->mm->mmap_sem);
|
---|
265 | do_munmap(pTask->mm, (unsigned long)pv, cb, 0); /* should it be 1 or 0? */
|
---|
266 | up_write(&pTask->mm->mmap_sem);
|
---|
267 | #else
|
---|
268 | down_write(&pTask->mm->mmap_sem);
|
---|
269 | do_munmap(pTask->mm, (unsigned long)pv, cb);
|
---|
270 | up_write(&pTask->mm->mmap_sem);
|
---|
271 | #endif
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Internal worker that allocates physical pages and creates the memory object for them.
|
---|
277 | *
|
---|
278 | * @returns IPRT status code.
|
---|
279 | * @param ppMemLnx Where to store the memory object pointer.
|
---|
280 | * @param enmType The object type.
|
---|
281 | * @param cb The number of bytes to allocate.
|
---|
282 | * @param uAlignment The alignment of the physical memory.
|
---|
283 | * Only valid if fContiguous == true, ignored otherwise.
|
---|
284 | * @param fFlagsLnx The page allocation flags (GPFs).
|
---|
285 | * @param fContiguous Whether the allocation must be contiguous.
|
---|
286 | * @param rcNoMem What to return when we're out of pages.
|
---|
287 | */
|
---|
288 | static int rtR0MemObjLinuxAllocPages(PRTR0MEMOBJLNX *ppMemLnx, RTR0MEMOBJTYPE enmType, size_t cb,
|
---|
289 | size_t uAlignment, unsigned fFlagsLnx, bool fContiguous, int rcNoMem)
|
---|
290 | {
|
---|
291 | size_t iPage;
|
---|
292 | size_t const cPages = cb >> PAGE_SHIFT;
|
---|
293 | struct page *paPages;
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Allocate a memory object structure that's large enough to contain
|
---|
297 | * the page pointer array.
|
---|
298 | */
|
---|
299 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJLNX, apPages[cPages]), enmType, NULL, cb);
|
---|
300 | if (!pMemLnx)
|
---|
301 | return VERR_NO_MEMORY;
|
---|
302 | pMemLnx->cPages = cPages;
|
---|
303 |
|
---|
304 | if (cPages > 255)
|
---|
305 | {
|
---|
306 | # ifdef __GFP_REPEAT
|
---|
307 | /* Try hard to allocate the memory, but the allocation attempt might fail. */
|
---|
308 | fFlagsLnx |= __GFP_REPEAT;
|
---|
309 | # endif
|
---|
310 | # ifdef __GFP_NOMEMALLOC
|
---|
311 | /* Introduced with Linux 2.6.12: Don't use emergency reserves */
|
---|
312 | fFlagsLnx |= __GFP_NOMEMALLOC;
|
---|
313 | # endif
|
---|
314 | }
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * Allocate the pages.
|
---|
318 | * For small allocations we'll try contiguous first and then fall back on page by page.
|
---|
319 | */
|
---|
320 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
321 | if ( fContiguous
|
---|
322 | || cb <= PAGE_SIZE * 2)
|
---|
323 | {
|
---|
324 | # ifdef VBOX_USE_INSERT_PAGE
|
---|
325 | paPages = alloc_pages(fFlagsLnx | __GFP_COMP | __GFP_NOWARN, rtR0MemObjLinuxOrder(cPages));
|
---|
326 | # else
|
---|
327 | paPages = alloc_pages(fFlagsLnx | __GFP_NOWARN, rtR0MemObjLinuxOrder(cPages));
|
---|
328 | # endif
|
---|
329 | if (paPages)
|
---|
330 | {
|
---|
331 | fContiguous = true;
|
---|
332 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
333 | pMemLnx->apPages[iPage] = &paPages[iPage];
|
---|
334 | }
|
---|
335 | else if (fContiguous)
|
---|
336 | {
|
---|
337 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
338 | return rcNoMem;
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | if (!fContiguous)
|
---|
343 | {
|
---|
344 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
345 | {
|
---|
346 | pMemLnx->apPages[iPage] = alloc_page(fFlagsLnx | __GFP_NOWARN);
|
---|
347 | if (RT_UNLIKELY(!pMemLnx->apPages[iPage]))
|
---|
348 | {
|
---|
349 | while (iPage-- > 0)
|
---|
350 | __free_page(pMemLnx->apPages[iPage]);
|
---|
351 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
352 | return rcNoMem;
|
---|
353 | }
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | #else /* < 2.4.22 */
|
---|
358 | /** @todo figure out why we didn't allocate page-by-page on 2.4.21 and older... */
|
---|
359 | paPages = alloc_pages(fFlagsLnx, rtR0MemObjLinuxOrder(cPages));
|
---|
360 | if (!paPages)
|
---|
361 | {
|
---|
362 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
363 | return rcNoMem;
|
---|
364 | }
|
---|
365 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
366 | {
|
---|
367 | pMemLnx->apPages[iPage] = &paPages[iPage];
|
---|
368 | MY_SET_PAGES_EXEC(pMemLnx->apPages[iPage], 1);
|
---|
369 | if (PageHighMem(pMemLnx->apPages[iPage]))
|
---|
370 | BUG();
|
---|
371 | }
|
---|
372 |
|
---|
373 | fContiguous = true;
|
---|
374 | #endif /* < 2.4.22 */
|
---|
375 | pMemLnx->fContiguous = fContiguous;
|
---|
376 |
|
---|
377 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
|
---|
378 | /*
|
---|
379 | * Reserve the pages.
|
---|
380 | *
|
---|
381 | * Linux >= 4.5 with CONFIG_DEBUG_VM panics when setting PG_reserved on compound
|
---|
382 | * pages. According to Michal Hocko this shouldn't be necessary anyway because
|
---|
383 | * as pages which are not on the LRU list are never evictable.
|
---|
384 | */
|
---|
385 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
386 | SetPageReserved(pMemLnx->apPages[iPage]);
|
---|
387 | #endif
|
---|
388 |
|
---|
389 | /*
|
---|
390 | * Note that the physical address of memory allocated with alloc_pages(flags, order)
|
---|
391 | * is always 2^(PAGE_SHIFT+order)-aligned.
|
---|
392 | */
|
---|
393 | if ( fContiguous
|
---|
394 | && uAlignment > PAGE_SIZE)
|
---|
395 | {
|
---|
396 | /*
|
---|
397 | * Check for alignment constraints. The physical address of memory allocated with
|
---|
398 | * alloc_pages(flags, order) is always 2^(PAGE_SHIFT+order)-aligned.
|
---|
399 | */
|
---|
400 | if (RT_UNLIKELY(page_to_phys(pMemLnx->apPages[0]) & (uAlignment - 1)))
|
---|
401 | {
|
---|
402 | /*
|
---|
403 | * This should never happen!
|
---|
404 | */
|
---|
405 | printk("rtR0MemObjLinuxAllocPages(cb=0x%lx, uAlignment=0x%lx): alloc_pages(..., %d) returned physical memory at 0x%lx!\n",
|
---|
406 | (unsigned long)cb, (unsigned long)uAlignment, rtR0MemObjLinuxOrder(cPages), (unsigned long)page_to_phys(pMemLnx->apPages[0]));
|
---|
407 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
408 | return rcNoMem;
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 | *ppMemLnx = pMemLnx;
|
---|
413 | return VINF_SUCCESS;
|
---|
414 | }
|
---|
415 |
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Frees the physical pages allocated by the rtR0MemObjLinuxAllocPages() call.
|
---|
419 | *
|
---|
420 | * This method does NOT free the object.
|
---|
421 | *
|
---|
422 | * @param pMemLnx The object which physical pages should be freed.
|
---|
423 | */
|
---|
424 | static void rtR0MemObjLinuxFreePages(PRTR0MEMOBJLNX pMemLnx)
|
---|
425 | {
|
---|
426 | size_t iPage = pMemLnx->cPages;
|
---|
427 | if (iPage > 0)
|
---|
428 | {
|
---|
429 | /*
|
---|
430 | * Restore the page flags.
|
---|
431 | */
|
---|
432 | while (iPage-- > 0)
|
---|
433 | {
|
---|
434 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
|
---|
435 | /*
|
---|
436 | * See SetPageReserved() in rtR0MemObjLinuxAllocPages()
|
---|
437 | */
|
---|
438 | ClearPageReserved(pMemLnx->apPages[iPage]);
|
---|
439 | #endif
|
---|
440 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
441 | #else
|
---|
442 | MY_SET_PAGES_NOEXEC(pMemLnx->apPages[iPage], 1);
|
---|
443 | #endif
|
---|
444 | }
|
---|
445 |
|
---|
446 | /*
|
---|
447 | * Free the pages.
|
---|
448 | */
|
---|
449 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
450 | if (!pMemLnx->fContiguous)
|
---|
451 | {
|
---|
452 | iPage = pMemLnx->cPages;
|
---|
453 | while (iPage-- > 0)
|
---|
454 | __free_page(pMemLnx->apPages[iPage]);
|
---|
455 | }
|
---|
456 | else
|
---|
457 | #endif
|
---|
458 | __free_pages(pMemLnx->apPages[0], rtR0MemObjLinuxOrder(pMemLnx->cPages));
|
---|
459 |
|
---|
460 | pMemLnx->cPages = 0;
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Maps the allocation into ring-0.
|
---|
467 | *
|
---|
468 | * This will update the RTR0MEMOBJLNX::Core.pv and RTR0MEMOBJ::fMappedToRing0 members.
|
---|
469 | *
|
---|
470 | * Contiguous mappings that isn't in 'high' memory will already be mapped into kernel
|
---|
471 | * space, so we'll use that mapping if possible. If execute access is required, we'll
|
---|
472 | * play safe and do our own mapping.
|
---|
473 | *
|
---|
474 | * @returns IPRT status code.
|
---|
475 | * @param pMemLnx The linux memory object to map.
|
---|
476 | * @param fExecutable Whether execute access is required.
|
---|
477 | */
|
---|
478 | static int rtR0MemObjLinuxVMap(PRTR0MEMOBJLNX pMemLnx, bool fExecutable)
|
---|
479 | {
|
---|
480 | int rc = VINF_SUCCESS;
|
---|
481 |
|
---|
482 | /*
|
---|
483 | * Choose mapping strategy.
|
---|
484 | */
|
---|
485 | bool fMustMap = fExecutable
|
---|
486 | || !pMemLnx->fContiguous;
|
---|
487 | if (!fMustMap)
|
---|
488 | {
|
---|
489 | size_t iPage = pMemLnx->cPages;
|
---|
490 | while (iPage-- > 0)
|
---|
491 | if (PageHighMem(pMemLnx->apPages[iPage]))
|
---|
492 | {
|
---|
493 | fMustMap = true;
|
---|
494 | break;
|
---|
495 | }
|
---|
496 | }
|
---|
497 |
|
---|
498 | Assert(!pMemLnx->Core.pv);
|
---|
499 | Assert(!pMemLnx->fMappedToRing0);
|
---|
500 |
|
---|
501 | if (fMustMap)
|
---|
502 | {
|
---|
503 | /*
|
---|
504 | * Use vmap - 2.4.22 and later.
|
---|
505 | */
|
---|
506 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
507 | pgprot_t fPg;
|
---|
508 | pgprot_val(fPg) = _PAGE_PRESENT | _PAGE_RW;
|
---|
509 | # ifdef _PAGE_NX
|
---|
510 | if (!fExecutable)
|
---|
511 | pgprot_val(fPg) |= _PAGE_NX;
|
---|
512 | # endif
|
---|
513 |
|
---|
514 | # ifdef VM_MAP
|
---|
515 | pMemLnx->Core.pv = vmap(&pMemLnx->apPages[0], pMemLnx->cPages, VM_MAP, fPg);
|
---|
516 | # else
|
---|
517 | pMemLnx->Core.pv = vmap(&pMemLnx->apPages[0], pMemLnx->cPages, VM_ALLOC, fPg);
|
---|
518 | # endif
|
---|
519 | if (pMemLnx->Core.pv)
|
---|
520 | pMemLnx->fMappedToRing0 = true;
|
---|
521 | else
|
---|
522 | rc = VERR_MAP_FAILED;
|
---|
523 | #else /* < 2.4.22 */
|
---|
524 | rc = VERR_NOT_SUPPORTED;
|
---|
525 | #endif
|
---|
526 | }
|
---|
527 | else
|
---|
528 | {
|
---|
529 | /*
|
---|
530 | * Use the kernel RAM mapping.
|
---|
531 | */
|
---|
532 | pMemLnx->Core.pv = phys_to_virt(page_to_phys(pMemLnx->apPages[0]));
|
---|
533 | Assert(pMemLnx->Core.pv);
|
---|
534 | }
|
---|
535 |
|
---|
536 | return rc;
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | /**
|
---|
541 | * Undoes what rtR0MemObjLinuxVMap() did.
|
---|
542 | *
|
---|
543 | * @param pMemLnx The linux memory object.
|
---|
544 | */
|
---|
545 | static void rtR0MemObjLinuxVUnmap(PRTR0MEMOBJLNX pMemLnx)
|
---|
546 | {
|
---|
547 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
548 | if (pMemLnx->fMappedToRing0)
|
---|
549 | {
|
---|
550 | Assert(pMemLnx->Core.pv);
|
---|
551 | vunmap(pMemLnx->Core.pv);
|
---|
552 | pMemLnx->fMappedToRing0 = false;
|
---|
553 | }
|
---|
554 | #else /* < 2.4.22 */
|
---|
555 | Assert(!pMemLnx->fMappedToRing0);
|
---|
556 | #endif
|
---|
557 | pMemLnx->Core.pv = NULL;
|
---|
558 | }
|
---|
559 |
|
---|
560 |
|
---|
561 | DECLHIDDEN(int) rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
|
---|
562 | {
|
---|
563 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
564 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
|
---|
565 |
|
---|
566 | /*
|
---|
567 | * Release any memory that we've allocated or locked.
|
---|
568 | */
|
---|
569 | switch (pMemLnx->Core.enmType)
|
---|
570 | {
|
---|
571 | case RTR0MEMOBJTYPE_LOW:
|
---|
572 | case RTR0MEMOBJTYPE_PAGE:
|
---|
573 | case RTR0MEMOBJTYPE_CONT:
|
---|
574 | case RTR0MEMOBJTYPE_PHYS:
|
---|
575 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
576 | rtR0MemObjLinuxVUnmap(pMemLnx);
|
---|
577 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
578 | break;
|
---|
579 |
|
---|
580 | case RTR0MEMOBJTYPE_LOCK:
|
---|
581 | if (pMemLnx->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
|
---|
582 | {
|
---|
583 | struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
|
---|
584 | size_t iPage;
|
---|
585 | Assert(pTask);
|
---|
586 | if (pTask && pTask->mm)
|
---|
587 | down_read(&pTask->mm->mmap_sem);
|
---|
588 |
|
---|
589 | iPage = pMemLnx->cPages;
|
---|
590 | while (iPage-- > 0)
|
---|
591 | {
|
---|
592 | if (!PageReserved(pMemLnx->apPages[iPage]))
|
---|
593 | SetPageDirty(pMemLnx->apPages[iPage]);
|
---|
594 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 6, 0)
|
---|
595 | put_page(pMemLnx->apPages[iPage]);
|
---|
596 | #else
|
---|
597 | page_cache_release(pMemLnx->apPages[iPage]);
|
---|
598 | #endif
|
---|
599 | }
|
---|
600 |
|
---|
601 | if (pTask && pTask->mm)
|
---|
602 | up_read(&pTask->mm->mmap_sem);
|
---|
603 | }
|
---|
604 | /* else: kernel memory - nothing to do here. */
|
---|
605 | break;
|
---|
606 |
|
---|
607 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
608 | Assert(pMemLnx->Core.pv);
|
---|
609 | if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
|
---|
610 | {
|
---|
611 | struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
|
---|
612 | Assert(pTask);
|
---|
613 | if (pTask && pTask->mm)
|
---|
614 | rtR0MemObjLinuxDoMunmap(pMemLnx->Core.pv, pMemLnx->Core.cb, pTask);
|
---|
615 | }
|
---|
616 | else
|
---|
617 | {
|
---|
618 | vunmap(pMemLnx->Core.pv);
|
---|
619 |
|
---|
620 | Assert(pMemLnx->cPages == 1 && pMemLnx->apPages[0] != NULL);
|
---|
621 | __free_page(pMemLnx->apPages[0]);
|
---|
622 | pMemLnx->apPages[0] = NULL;
|
---|
623 | pMemLnx->cPages = 0;
|
---|
624 | }
|
---|
625 | pMemLnx->Core.pv = NULL;
|
---|
626 | break;
|
---|
627 |
|
---|
628 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
629 | Assert(pMemLnx->cPages == 0); Assert(pMemLnx->Core.pv);
|
---|
630 | if (pMemLnx->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
|
---|
631 | {
|
---|
632 | struct task_struct *pTask = rtR0ProcessToLinuxTask(pMemLnx->Core.u.Lock.R0Process);
|
---|
633 | Assert(pTask);
|
---|
634 | if (pTask && pTask->mm)
|
---|
635 | rtR0MemObjLinuxDoMunmap(pMemLnx->Core.pv, pMemLnx->Core.cb, pTask);
|
---|
636 | }
|
---|
637 | else
|
---|
638 | vunmap(pMemLnx->Core.pv);
|
---|
639 | pMemLnx->Core.pv = NULL;
|
---|
640 | break;
|
---|
641 |
|
---|
642 | default:
|
---|
643 | AssertMsgFailed(("enmType=%d\n", pMemLnx->Core.enmType));
|
---|
644 | return VERR_INTERNAL_ERROR;
|
---|
645 | }
|
---|
646 | IPRT_LINUX_RESTORE_EFL_ONLY_AC();
|
---|
647 | return VINF_SUCCESS;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
652 | {
|
---|
653 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
654 | PRTR0MEMOBJLNX pMemLnx;
|
---|
655 | int rc;
|
---|
656 |
|
---|
657 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
658 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, PAGE_SIZE, GFP_HIGHUSER,
|
---|
659 | false /* non-contiguous */, VERR_NO_MEMORY);
|
---|
660 | #else
|
---|
661 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_PAGE, cb, PAGE_SIZE, GFP_USER,
|
---|
662 | false /* non-contiguous */, VERR_NO_MEMORY);
|
---|
663 | #endif
|
---|
664 | if (RT_SUCCESS(rc))
|
---|
665 | {
|
---|
666 | rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
|
---|
667 | if (RT_SUCCESS(rc))
|
---|
668 | {
|
---|
669 | *ppMem = &pMemLnx->Core;
|
---|
670 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
671 | return rc;
|
---|
672 | }
|
---|
673 |
|
---|
674 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
675 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
676 | }
|
---|
677 |
|
---|
678 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
679 | return rc;
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
684 | {
|
---|
685 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
686 | PRTR0MEMOBJLNX pMemLnx;
|
---|
687 | int rc;
|
---|
688 |
|
---|
689 | /* Try to avoid GFP_DMA. GFM_DMA32 was introduced with Linux 2.6.15. */
|
---|
690 | #if (defined(RT_ARCH_AMD64) || defined(CONFIG_X86_PAE)) && defined(GFP_DMA32)
|
---|
691 | /* ZONE_DMA32: 0-4GB */
|
---|
692 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, PAGE_SIZE, GFP_DMA32,
|
---|
693 | false /* non-contiguous */, VERR_NO_LOW_MEMORY);
|
---|
694 | if (RT_FAILURE(rc))
|
---|
695 | #endif
|
---|
696 | #ifdef RT_ARCH_AMD64
|
---|
697 | /* ZONE_DMA: 0-16MB */
|
---|
698 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, PAGE_SIZE, GFP_DMA,
|
---|
699 | false /* non-contiguous */, VERR_NO_LOW_MEMORY);
|
---|
700 | #else
|
---|
701 | # ifdef CONFIG_X86_PAE
|
---|
702 | # endif
|
---|
703 | /* ZONE_NORMAL: 0-896MB */
|
---|
704 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_LOW, cb, PAGE_SIZE, GFP_USER,
|
---|
705 | false /* non-contiguous */, VERR_NO_LOW_MEMORY);
|
---|
706 | #endif
|
---|
707 | if (RT_SUCCESS(rc))
|
---|
708 | {
|
---|
709 | rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
|
---|
710 | if (RT_SUCCESS(rc))
|
---|
711 | {
|
---|
712 | *ppMem = &pMemLnx->Core;
|
---|
713 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
714 | return rc;
|
---|
715 | }
|
---|
716 |
|
---|
717 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
718 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
719 | }
|
---|
720 |
|
---|
721 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
722 | return rc;
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
727 | {
|
---|
728 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
729 | PRTR0MEMOBJLNX pMemLnx;
|
---|
730 | int rc;
|
---|
731 |
|
---|
732 | #if (defined(RT_ARCH_AMD64) || defined(CONFIG_X86_PAE)) && defined(GFP_DMA32)
|
---|
733 | /* ZONE_DMA32: 0-4GB */
|
---|
734 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, PAGE_SIZE, GFP_DMA32,
|
---|
735 | true /* contiguous */, VERR_NO_CONT_MEMORY);
|
---|
736 | if (RT_FAILURE(rc))
|
---|
737 | #endif
|
---|
738 | #ifdef RT_ARCH_AMD64
|
---|
739 | /* ZONE_DMA: 0-16MB */
|
---|
740 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, PAGE_SIZE, GFP_DMA,
|
---|
741 | true /* contiguous */, VERR_NO_CONT_MEMORY);
|
---|
742 | #else
|
---|
743 | /* ZONE_NORMAL (32-bit hosts): 0-896MB */
|
---|
744 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, RTR0MEMOBJTYPE_CONT, cb, PAGE_SIZE, GFP_USER,
|
---|
745 | true /* contiguous */, VERR_NO_CONT_MEMORY);
|
---|
746 | #endif
|
---|
747 | if (RT_SUCCESS(rc))
|
---|
748 | {
|
---|
749 | rc = rtR0MemObjLinuxVMap(pMemLnx, fExecutable);
|
---|
750 | if (RT_SUCCESS(rc))
|
---|
751 | {
|
---|
752 | #if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(CONFIG_HIGHMEM64G))
|
---|
753 | size_t iPage = pMemLnx->cPages;
|
---|
754 | while (iPage-- > 0)
|
---|
755 | Assert(page_to_phys(pMemLnx->apPages[iPage]) < _4G);
|
---|
756 | #endif
|
---|
757 | pMemLnx->Core.u.Cont.Phys = page_to_phys(pMemLnx->apPages[0]);
|
---|
758 | *ppMem = &pMemLnx->Core;
|
---|
759 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
760 | return rc;
|
---|
761 | }
|
---|
762 |
|
---|
763 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
764 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
765 | }
|
---|
766 |
|
---|
767 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
768 | return rc;
|
---|
769 | }
|
---|
770 |
|
---|
771 |
|
---|
772 | /**
|
---|
773 | * Worker for rtR0MemObjLinuxAllocPhysSub that tries one allocation strategy.
|
---|
774 | *
|
---|
775 | * @returns IPRT status code.
|
---|
776 | * @param ppMemLnx Where to
|
---|
777 | * @param enmType The object type.
|
---|
778 | * @param cb The size of the allocation.
|
---|
779 | * @param uAlignment The alignment of the physical memory.
|
---|
780 | * Only valid for fContiguous == true, ignored otherwise.
|
---|
781 | * @param PhysHighest See rtR0MemObjNativeAllocPhys.
|
---|
782 | * @param fGfp The Linux GFP flags to use for the allocation.
|
---|
783 | */
|
---|
784 | static int rtR0MemObjLinuxAllocPhysSub2(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType,
|
---|
785 | size_t cb, size_t uAlignment, RTHCPHYS PhysHighest, unsigned fGfp)
|
---|
786 | {
|
---|
787 | PRTR0MEMOBJLNX pMemLnx;
|
---|
788 | int rc;
|
---|
789 |
|
---|
790 | rc = rtR0MemObjLinuxAllocPages(&pMemLnx, enmType, cb, uAlignment, fGfp,
|
---|
791 | enmType == RTR0MEMOBJTYPE_PHYS /* contiguous / non-contiguous */,
|
---|
792 | VERR_NO_PHYS_MEMORY);
|
---|
793 | if (RT_FAILURE(rc))
|
---|
794 | return rc;
|
---|
795 |
|
---|
796 | /*
|
---|
797 | * Check the addresses if necessary. (Can be optimized a bit for PHYS.)
|
---|
798 | */
|
---|
799 | if (PhysHighest != NIL_RTHCPHYS)
|
---|
800 | {
|
---|
801 | size_t iPage = pMemLnx->cPages;
|
---|
802 | while (iPage-- > 0)
|
---|
803 | if (page_to_phys(pMemLnx->apPages[iPage]) > PhysHighest)
|
---|
804 | {
|
---|
805 | rtR0MemObjLinuxFreePages(pMemLnx);
|
---|
806 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
807 | return VERR_NO_MEMORY;
|
---|
808 | }
|
---|
809 | }
|
---|
810 |
|
---|
811 | /*
|
---|
812 | * Complete the object.
|
---|
813 | */
|
---|
814 | if (enmType == RTR0MEMOBJTYPE_PHYS)
|
---|
815 | {
|
---|
816 | pMemLnx->Core.u.Phys.PhysBase = page_to_phys(pMemLnx->apPages[0]);
|
---|
817 | pMemLnx->Core.u.Phys.fAllocated = true;
|
---|
818 | }
|
---|
819 | *ppMem = &pMemLnx->Core;
|
---|
820 | return rc;
|
---|
821 | }
|
---|
822 |
|
---|
823 |
|
---|
824 | /**
|
---|
825 | * Worker for rtR0MemObjNativeAllocPhys and rtR0MemObjNativeAllocPhysNC.
|
---|
826 | *
|
---|
827 | * @returns IPRT status code.
|
---|
828 | * @param ppMem Where to store the memory object pointer on success.
|
---|
829 | * @param enmType The object type.
|
---|
830 | * @param cb The size of the allocation.
|
---|
831 | * @param uAlignment The alignment of the physical memory.
|
---|
832 | * Only valid for enmType == RTR0MEMOBJTYPE_PHYS, ignored otherwise.
|
---|
833 | * @param PhysHighest See rtR0MemObjNativeAllocPhys.
|
---|
834 | */
|
---|
835 | static int rtR0MemObjLinuxAllocPhysSub(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType,
|
---|
836 | size_t cb, size_t uAlignment, RTHCPHYS PhysHighest)
|
---|
837 | {
|
---|
838 | int rc;
|
---|
839 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
840 |
|
---|
841 | /*
|
---|
842 | * There are two clear cases and that's the <=16MB and anything-goes ones.
|
---|
843 | * When the physical address limit is somewhere in-between those two we'll
|
---|
844 | * just have to try, starting with HIGHUSER and working our way thru the
|
---|
845 | * different types, hoping we'll get lucky.
|
---|
846 | *
|
---|
847 | * We should probably move this physical address restriction logic up to
|
---|
848 | * the page alloc function as it would be more efficient there. But since
|
---|
849 | * we don't expect this to be a performance issue just yet it can wait.
|
---|
850 | */
|
---|
851 | if (PhysHighest == NIL_RTHCPHYS)
|
---|
852 | /* ZONE_HIGHMEM: the whole physical memory */
|
---|
853 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_HIGHUSER);
|
---|
854 | else if (PhysHighest <= _1M * 16)
|
---|
855 | /* ZONE_DMA: 0-16MB */
|
---|
856 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_DMA);
|
---|
857 | else
|
---|
858 | {
|
---|
859 | rc = VERR_NO_MEMORY;
|
---|
860 | if (RT_FAILURE(rc))
|
---|
861 | /* ZONE_HIGHMEM: the whole physical memory */
|
---|
862 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_HIGHUSER);
|
---|
863 | if (RT_FAILURE(rc))
|
---|
864 | /* ZONE_NORMAL: 0-896MB */
|
---|
865 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_USER);
|
---|
866 | #ifdef GFP_DMA32
|
---|
867 | if (RT_FAILURE(rc))
|
---|
868 | /* ZONE_DMA32: 0-4GB */
|
---|
869 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_DMA32);
|
---|
870 | #endif
|
---|
871 | if (RT_FAILURE(rc))
|
---|
872 | /* ZONE_DMA: 0-16MB */
|
---|
873 | rc = rtR0MemObjLinuxAllocPhysSub2(ppMem, enmType, cb, uAlignment, PhysHighest, GFP_DMA);
|
---|
874 | }
|
---|
875 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
876 | return rc;
|
---|
877 | }
|
---|
878 |
|
---|
879 |
|
---|
880 | /**
|
---|
881 | * Translates a kernel virtual address to a linux page structure by walking the
|
---|
882 | * page tables.
|
---|
883 | *
|
---|
884 | * @note We do assume that the page tables will not change as we are walking
|
---|
885 | * them. This assumption is rather forced by the fact that I could not
|
---|
886 | * immediately see any way of preventing this from happening. So, we
|
---|
887 | * take some extra care when accessing them.
|
---|
888 | *
|
---|
889 | * Because of this, we don't want to use this function on memory where
|
---|
890 | * attribute changes to nearby pages is likely to cause large pages to
|
---|
891 | * be used or split up. So, don't use this for the linear mapping of
|
---|
892 | * physical memory.
|
---|
893 | *
|
---|
894 | * @returns Pointer to the page structur or NULL if it could not be found.
|
---|
895 | * @param pv The kernel virtual address.
|
---|
896 | */
|
---|
897 | static struct page *rtR0MemObjLinuxVirtToPage(void *pv)
|
---|
898 | {
|
---|
899 | unsigned long ulAddr = (unsigned long)pv;
|
---|
900 | unsigned long pfn;
|
---|
901 | struct page *pPage;
|
---|
902 | pte_t *pEntry;
|
---|
903 | union
|
---|
904 | {
|
---|
905 | pgd_t Global;
|
---|
906 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
|
---|
907 | p4d_t Four;
|
---|
908 | #endif
|
---|
909 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
|
---|
910 | pud_t Upper;
|
---|
911 | #endif
|
---|
912 | pmd_t Middle;
|
---|
913 | pte_t Entry;
|
---|
914 | } u;
|
---|
915 |
|
---|
916 | /* Should this happen in a situation this code will be called in? And if
|
---|
917 | * so, can it change under our feet? See also
|
---|
918 | * "Documentation/vm/active_mm.txt" in the kernel sources. */
|
---|
919 | if (RT_UNLIKELY(!current->active_mm))
|
---|
920 | return NULL;
|
---|
921 | u.Global = *pgd_offset(current->active_mm, ulAddr);
|
---|
922 | if (RT_UNLIKELY(pgd_none(u.Global)))
|
---|
923 | return NULL;
|
---|
924 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
|
---|
925 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
|
---|
926 | u.Four = *p4d_offset(&u.Global, ulAddr);
|
---|
927 | if (RT_UNLIKELY(p4d_none(u.Four)))
|
---|
928 | return NULL;
|
---|
929 | if (p4d_large(u.Four))
|
---|
930 | {
|
---|
931 | pPage = p4d_page(u.Four);
|
---|
932 | AssertReturn(pPage, NULL);
|
---|
933 | pfn = page_to_pfn(pPage); /* doing the safe way... */
|
---|
934 | AssertCompile(P4D_SHIFT - PAGE_SHIFT < 31);
|
---|
935 | pfn += (ulAddr >> PAGE_SHIFT) & ((UINT32_C(1) << (P4D_SHIFT - PAGE_SHIFT)) - 1);
|
---|
936 | return pfn_to_page(pfn);
|
---|
937 | }
|
---|
938 | u.Upper = *pud_offset(&u.Four, ulAddr);
|
---|
939 | # else /* < 4.12 */
|
---|
940 | u.Upper = *pud_offset(&u.Global, ulAddr);
|
---|
941 | # endif /* < 4.12 */
|
---|
942 | if (RT_UNLIKELY(pud_none(u.Upper)))
|
---|
943 | return NULL;
|
---|
944 | # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
|
---|
945 | if (pud_large(u.Upper))
|
---|
946 | {
|
---|
947 | pPage = pud_page(u.Upper);
|
---|
948 | AssertReturn(pPage, NULL);
|
---|
949 | pfn = page_to_pfn(pPage); /* doing the safe way... */
|
---|
950 | pfn += (ulAddr >> PAGE_SHIFT) & ((UINT32_C(1) << (PUD_SHIFT - PAGE_SHIFT)) - 1);
|
---|
951 | return pfn_to_page(pfn);
|
---|
952 | }
|
---|
953 | # endif
|
---|
954 | u.Middle = *pmd_offset(&u.Upper, ulAddr);
|
---|
955 | #else /* < 2.6.11 */
|
---|
956 | u.Middle = *pmd_offset(&u.Global, ulAddr);
|
---|
957 | #endif /* < 2.6.11 */
|
---|
958 | if (RT_UNLIKELY(pmd_none(u.Middle)))
|
---|
959 | return NULL;
|
---|
960 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
|
---|
961 | if (pmd_large(u.Middle))
|
---|
962 | {
|
---|
963 | pPage = pmd_page(u.Middle);
|
---|
964 | AssertReturn(pPage, NULL);
|
---|
965 | pfn = page_to_pfn(pPage); /* doing the safe way... */
|
---|
966 | pfn += (ulAddr >> PAGE_SHIFT) & ((UINT32_C(1) << (PMD_SHIFT - PAGE_SHIFT)) - 1);
|
---|
967 | return pfn_to_page(pfn);
|
---|
968 | }
|
---|
969 | #endif
|
---|
970 |
|
---|
971 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 5) || defined(pte_offset_map) /* As usual, RHEL 3 had pte_offset_map earlier. */
|
---|
972 | pEntry = pte_offset_map(&u.Middle, ulAddr);
|
---|
973 | #else
|
---|
974 | pEntry = pte_offset(&u.Middle, ulAddr);
|
---|
975 | #endif
|
---|
976 | if (RT_UNLIKELY(!pEntry))
|
---|
977 | return NULL;
|
---|
978 | u.Entry = *pEntry;
|
---|
979 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 5) || defined(pte_offset_map)
|
---|
980 | pte_unmap(pEntry);
|
---|
981 | #endif
|
---|
982 |
|
---|
983 | if (RT_UNLIKELY(!pte_present(u.Entry)))
|
---|
984 | return NULL;
|
---|
985 | return pte_page(u.Entry);
|
---|
986 | }
|
---|
987 |
|
---|
988 |
|
---|
989 | DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
|
---|
990 | {
|
---|
991 | return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS, cb, uAlignment, PhysHighest);
|
---|
992 | }
|
---|
993 |
|
---|
994 |
|
---|
995 | DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
996 | {
|
---|
997 | return rtR0MemObjLinuxAllocPhysSub(ppMem, RTR0MEMOBJTYPE_PHYS_NC, cb, PAGE_SIZE, PhysHighest);
|
---|
998 | }
|
---|
999 |
|
---|
1000 |
|
---|
1001 | DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
|
---|
1002 | {
|
---|
1003 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
1004 |
|
---|
1005 | /*
|
---|
1006 | * All we need to do here is to validate that we can use
|
---|
1007 | * ioremap on the specified address (32/64-bit dma_addr_t).
|
---|
1008 | */
|
---|
1009 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1010 | dma_addr_t PhysAddr = Phys;
|
---|
1011 | AssertMsgReturn(PhysAddr == Phys, ("%#llx\n", (unsigned long long)Phys), VERR_ADDRESS_TOO_BIG);
|
---|
1012 |
|
---|
1013 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
1014 | if (!pMemLnx)
|
---|
1015 | {
|
---|
1016 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1017 | return VERR_NO_MEMORY;
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | pMemLnx->Core.u.Phys.PhysBase = PhysAddr;
|
---|
1021 | pMemLnx->Core.u.Phys.fAllocated = false;
|
---|
1022 | pMemLnx->Core.u.Phys.uCachePolicy = uCachePolicy;
|
---|
1023 | Assert(!pMemLnx->cPages);
|
---|
1024 | *ppMem = &pMemLnx->Core;
|
---|
1025 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1026 | return VINF_SUCCESS;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | /* openSUSE Leap 42.3 detection :-/ */
|
---|
1030 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0) \
|
---|
1031 | && LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0) \
|
---|
1032 | && defined(FAULT_FLAG_REMOTE)
|
---|
1033 | # define GET_USER_PAGES_API KERNEL_VERSION(4, 10, 0) /* no typo! */
|
---|
1034 | #else
|
---|
1035 | # define GET_USER_PAGES_API LINUX_VERSION_CODE
|
---|
1036 | #endif
|
---|
1037 |
|
---|
1038 | DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
|
---|
1039 | {
|
---|
1040 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
1041 | const int cPages = cb >> PAGE_SHIFT;
|
---|
1042 | struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
|
---|
1043 | struct vm_area_struct **papVMAs;
|
---|
1044 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1045 | int rc = VERR_NO_MEMORY;
|
---|
1046 | int const fWrite = fAccess & RTMEM_PROT_WRITE ? 1 : 0;
|
---|
1047 |
|
---|
1048 | /*
|
---|
1049 | * Check for valid task and size overflows.
|
---|
1050 | */
|
---|
1051 | if (!pTask)
|
---|
1052 | return VERR_NOT_SUPPORTED;
|
---|
1053 | if (((size_t)cPages << PAGE_SHIFT) != cb)
|
---|
1054 | return VERR_OUT_OF_RANGE;
|
---|
1055 |
|
---|
1056 | /*
|
---|
1057 | * Allocate the memory object and a temporary buffer for the VMAs.
|
---|
1058 | */
|
---|
1059 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
|
---|
1060 | if (!pMemLnx)
|
---|
1061 | {
|
---|
1062 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1063 | return VERR_NO_MEMORY;
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | papVMAs = (struct vm_area_struct **)RTMemAlloc(sizeof(*papVMAs) * cPages);
|
---|
1067 | if (papVMAs)
|
---|
1068 | {
|
---|
1069 | down_read(&pTask->mm->mmap_sem);
|
---|
1070 |
|
---|
1071 | /*
|
---|
1072 | * Get user pages.
|
---|
1073 | */
|
---|
1074 | #if GET_USER_PAGES_API >= KERNEL_VERSION(4, 6, 0)
|
---|
1075 | if (R0Process == RTR0ProcHandleSelf())
|
---|
1076 | rc = get_user_pages(R3Ptr, /* Where from. */
|
---|
1077 | cPages, /* How many pages. */
|
---|
1078 | # if GET_USER_PAGES_API >= KERNEL_VERSION(4, 9, 0)
|
---|
1079 | fWrite ? FOLL_WRITE | /* Write to memory. */
|
---|
1080 | FOLL_FORCE /* force write access. */
|
---|
1081 | : 0, /* Write to memory. */
|
---|
1082 | # else
|
---|
1083 | fWrite, /* Write to memory. */
|
---|
1084 | fWrite, /* force write access. */
|
---|
1085 | # endif
|
---|
1086 | &pMemLnx->apPages[0], /* Page array. */
|
---|
1087 | papVMAs); /* vmas */
|
---|
1088 | /*
|
---|
1089 | * Actually this should not happen at the moment as call this function
|
---|
1090 | * only for our own process.
|
---|
1091 | */
|
---|
1092 | else
|
---|
1093 | rc = get_user_pages_remote(
|
---|
1094 | pTask, /* Task for fault accounting. */
|
---|
1095 | pTask->mm, /* Whose pages. */
|
---|
1096 | R3Ptr, /* Where from. */
|
---|
1097 | cPages, /* How many pages. */
|
---|
1098 | # if GET_USER_PAGES_API >= KERNEL_VERSION(4, 9, 0)
|
---|
1099 | fWrite ? FOLL_WRITE | /* Write to memory. */
|
---|
1100 | FOLL_FORCE /* force write access. */
|
---|
1101 | : 0, /* Write to memory. */
|
---|
1102 | # else
|
---|
1103 | fWrite, /* Write to memory. */
|
---|
1104 | fWrite, /* force write access. */
|
---|
1105 | # endif
|
---|
1106 | &pMemLnx->apPages[0], /* Page array. */
|
---|
1107 | papVMAs /* vmas */
|
---|
1108 | # if GET_USER_PAGES_API >= KERNEL_VERSION(4, 10, 0)
|
---|
1109 | , NULL /* locked */
|
---|
1110 | # endif
|
---|
1111 | );
|
---|
1112 | #else /* GET_USER_PAGES_API < KERNEL_VERSION(4, 6, 0) */
|
---|
1113 | rc = get_user_pages(pTask, /* Task for fault accounting. */
|
---|
1114 | pTask->mm, /* Whose pages. */
|
---|
1115 | R3Ptr, /* Where from. */
|
---|
1116 | cPages, /* How many pages. */
|
---|
1117 | # if GET_USER_PAGES_API >= KERNEL_VERSION(4, 9, 0)
|
---|
1118 | fWrite ? FOLL_WRITE | /* Write to memory. */
|
---|
1119 | FOLL_FORCE /* force write access. */
|
---|
1120 | : 0, /* Write to memory. */
|
---|
1121 | # else
|
---|
1122 | fWrite, /* Write to memory. */
|
---|
1123 | fWrite, /* force write access. */
|
---|
1124 | # endif
|
---|
1125 | &pMemLnx->apPages[0], /* Page array. */
|
---|
1126 | papVMAs); /* vmas */
|
---|
1127 | #endif /* GET_USER_PAGES_API < KERNEL_VERSION(4, 6, 0) */
|
---|
1128 | if (rc == cPages)
|
---|
1129 | {
|
---|
1130 | /*
|
---|
1131 | * Flush dcache (required?), protect against fork and _really_ pin the page
|
---|
1132 | * table entries. get_user_pages() will protect against swapping out the
|
---|
1133 | * pages but it will NOT protect against removing page table entries. This
|
---|
1134 | * can be achieved with
|
---|
1135 | * - using mlock / mmap(..., MAP_LOCKED, ...) from userland. This requires
|
---|
1136 | * an appropriate limit set up with setrlimit(..., RLIMIT_MEMLOCK, ...).
|
---|
1137 | * Usual Linux distributions support only a limited size of locked pages
|
---|
1138 | * (e.g. 32KB).
|
---|
1139 | * - setting the PageReserved bit (as we do in rtR0MemObjLinuxAllocPages()
|
---|
1140 | * or by
|
---|
1141 | * - setting the VM_LOCKED flag. This is the same as doing mlock() without
|
---|
1142 | * a range check.
|
---|
1143 | */
|
---|
1144 | /** @todo The Linux fork() protection will require more work if this API
|
---|
1145 | * is to be used for anything but locking VM pages. */
|
---|
1146 | while (rc-- > 0)
|
---|
1147 | {
|
---|
1148 | flush_dcache_page(pMemLnx->apPages[rc]);
|
---|
1149 | papVMAs[rc]->vm_flags |= (VM_DONTCOPY | VM_LOCKED);
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | up_read(&pTask->mm->mmap_sem);
|
---|
1153 |
|
---|
1154 | RTMemFree(papVMAs);
|
---|
1155 |
|
---|
1156 | pMemLnx->Core.u.Lock.R0Process = R0Process;
|
---|
1157 | pMemLnx->cPages = cPages;
|
---|
1158 | Assert(!pMemLnx->fMappedToRing0);
|
---|
1159 | *ppMem = &pMemLnx->Core;
|
---|
1160 |
|
---|
1161 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1162 | return VINF_SUCCESS;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | /*
|
---|
1166 | * Failed - we need to unlock any pages that we succeeded to lock.
|
---|
1167 | */
|
---|
1168 | while (rc-- > 0)
|
---|
1169 | {
|
---|
1170 | if (!PageReserved(pMemLnx->apPages[rc]))
|
---|
1171 | SetPageDirty(pMemLnx->apPages[rc]);
|
---|
1172 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 6, 0)
|
---|
1173 | put_page(pMemLnx->apPages[rc]);
|
---|
1174 | #else
|
---|
1175 | page_cache_release(pMemLnx->apPages[rc]);
|
---|
1176 | #endif
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | up_read(&pTask->mm->mmap_sem);
|
---|
1180 |
|
---|
1181 | RTMemFree(papVMAs);
|
---|
1182 | rc = VERR_LOCK_FAILED;
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
1186 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1187 | return rc;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 |
|
---|
1191 | DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
|
---|
1192 | {
|
---|
1193 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
1194 | void *pvLast = (uint8_t *)pv + cb - 1;
|
---|
1195 | size_t const cPages = cb >> PAGE_SHIFT;
|
---|
1196 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1197 | bool fLinearMapping;
|
---|
1198 | int rc;
|
---|
1199 | uint8_t *pbPage;
|
---|
1200 | size_t iPage;
|
---|
1201 | NOREF(fAccess);
|
---|
1202 |
|
---|
1203 | if ( !RTR0MemKernelIsValidAddr(pv)
|
---|
1204 | || !RTR0MemKernelIsValidAddr(pv + cb))
|
---|
1205 | return VERR_INVALID_PARAMETER;
|
---|
1206 |
|
---|
1207 | /*
|
---|
1208 | * The lower part of the kernel memory has a linear mapping between
|
---|
1209 | * physical and virtual addresses. So we take a short cut here. This is
|
---|
1210 | * assumed to be the cleanest way to handle those addresses (and the code
|
---|
1211 | * is well tested, though the test for determining it is not very nice).
|
---|
1212 | * If we ever decide it isn't we can still remove it.
|
---|
1213 | */
|
---|
1214 | #if 0
|
---|
1215 | fLinearMapping = (unsigned long)pvLast < VMALLOC_START;
|
---|
1216 | #else
|
---|
1217 | fLinearMapping = (unsigned long)pv >= (unsigned long)__va(0)
|
---|
1218 | && (unsigned long)pvLast < (unsigned long)high_memory;
|
---|
1219 | #endif
|
---|
1220 |
|
---|
1221 | /*
|
---|
1222 | * Allocate the memory object.
|
---|
1223 | */
|
---|
1224 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJLNX, apPages[cPages]), RTR0MEMOBJTYPE_LOCK, pv, cb);
|
---|
1225 | if (!pMemLnx)
|
---|
1226 | {
|
---|
1227 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1228 | return VERR_NO_MEMORY;
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | /*
|
---|
1232 | * Gather the pages.
|
---|
1233 | * We ASSUME all kernel pages are non-swappable and non-movable.
|
---|
1234 | */
|
---|
1235 | rc = VINF_SUCCESS;
|
---|
1236 | pbPage = (uint8_t *)pvLast;
|
---|
1237 | iPage = cPages;
|
---|
1238 | if (!fLinearMapping)
|
---|
1239 | {
|
---|
1240 | while (iPage-- > 0)
|
---|
1241 | {
|
---|
1242 | struct page *pPage = rtR0MemObjLinuxVirtToPage(pbPage);
|
---|
1243 | if (RT_UNLIKELY(!pPage))
|
---|
1244 | {
|
---|
1245 | rc = VERR_LOCK_FAILED;
|
---|
1246 | break;
|
---|
1247 | }
|
---|
1248 | pMemLnx->apPages[iPage] = pPage;
|
---|
1249 | pbPage -= PAGE_SIZE;
|
---|
1250 | }
|
---|
1251 | }
|
---|
1252 | else
|
---|
1253 | {
|
---|
1254 | while (iPage-- > 0)
|
---|
1255 | {
|
---|
1256 | pMemLnx->apPages[iPage] = virt_to_page(pbPage);
|
---|
1257 | pbPage -= PAGE_SIZE;
|
---|
1258 | }
|
---|
1259 | }
|
---|
1260 | if (RT_SUCCESS(rc))
|
---|
1261 | {
|
---|
1262 | /*
|
---|
1263 | * Complete the memory object and return.
|
---|
1264 | */
|
---|
1265 | pMemLnx->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
|
---|
1266 | pMemLnx->cPages = cPages;
|
---|
1267 | Assert(!pMemLnx->fMappedToRing0);
|
---|
1268 | *ppMem = &pMemLnx->Core;
|
---|
1269 |
|
---|
1270 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1271 | return VINF_SUCCESS;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
1275 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1276 | return rc;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 |
|
---|
1280 | DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
|
---|
1281 | {
|
---|
1282 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
1283 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
1284 | const size_t cPages = cb >> PAGE_SHIFT;
|
---|
1285 | struct page *pDummyPage;
|
---|
1286 | struct page **papPages;
|
---|
1287 |
|
---|
1288 | /* check for unsupported stuff. */
|
---|
1289 | AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
|
---|
1290 | if (uAlignment > PAGE_SIZE)
|
---|
1291 | return VERR_NOT_SUPPORTED;
|
---|
1292 |
|
---|
1293 | /*
|
---|
1294 | * Allocate a dummy page and create a page pointer array for vmap such that
|
---|
1295 | * the dummy page is mapped all over the reserved area.
|
---|
1296 | */
|
---|
1297 | pDummyPage = alloc_page(GFP_HIGHUSER | __GFP_NOWARN);
|
---|
1298 | if (pDummyPage)
|
---|
1299 | {
|
---|
1300 | papPages = RTMemAlloc(sizeof(*papPages) * cPages);
|
---|
1301 | if (papPages)
|
---|
1302 | {
|
---|
1303 | void *pv;
|
---|
1304 | size_t iPage = cPages;
|
---|
1305 | while (iPage-- > 0)
|
---|
1306 | papPages[iPage] = pDummyPage;
|
---|
1307 | # ifdef VM_MAP
|
---|
1308 | pv = vmap(papPages, cPages, VM_MAP, PAGE_KERNEL_RO);
|
---|
1309 | # else
|
---|
1310 | pv = vmap(papPages, cPages, VM_ALLOC, PAGE_KERNEL_RO);
|
---|
1311 | # endif
|
---|
1312 | RTMemFree(papPages);
|
---|
1313 | if (pv)
|
---|
1314 | {
|
---|
1315 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
|
---|
1316 | if (pMemLnx)
|
---|
1317 | {
|
---|
1318 | pMemLnx->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
|
---|
1319 | pMemLnx->cPages = 1;
|
---|
1320 | pMemLnx->apPages[0] = pDummyPage;
|
---|
1321 | *ppMem = &pMemLnx->Core;
|
---|
1322 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1323 | return VINF_SUCCESS;
|
---|
1324 | }
|
---|
1325 | vunmap(pv);
|
---|
1326 | }
|
---|
1327 | }
|
---|
1328 | __free_page(pDummyPage);
|
---|
1329 | }
|
---|
1330 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1331 | return VERR_NO_MEMORY;
|
---|
1332 |
|
---|
1333 | #else /* < 2.4.22 */
|
---|
1334 | /*
|
---|
1335 | * Could probably use ioremap here, but the caller is in a better position than us
|
---|
1336 | * to select some safe physical memory.
|
---|
1337 | */
|
---|
1338 | return VERR_NOT_SUPPORTED;
|
---|
1339 | #endif
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 |
|
---|
1343 | DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
|
---|
1344 | {
|
---|
1345 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
1346 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1347 | void *pv;
|
---|
1348 | struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
|
---|
1349 | if (!pTask)
|
---|
1350 | return VERR_NOT_SUPPORTED;
|
---|
1351 |
|
---|
1352 | /*
|
---|
1353 | * Check that the specified alignment is supported.
|
---|
1354 | */
|
---|
1355 | if (uAlignment > PAGE_SIZE)
|
---|
1356 | return VERR_NOT_SUPPORTED;
|
---|
1357 |
|
---|
1358 | /*
|
---|
1359 | * Let rtR0MemObjLinuxDoMmap do the difficult bits.
|
---|
1360 | */
|
---|
1361 | pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, cb, uAlignment, pTask, RTMEM_PROT_NONE);
|
---|
1362 | if (pv == (void *)-1)
|
---|
1363 | {
|
---|
1364 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1365 | return VERR_NO_MEMORY;
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
|
---|
1369 | if (!pMemLnx)
|
---|
1370 | {
|
---|
1371 | rtR0MemObjLinuxDoMunmap(pv, cb, pTask);
|
---|
1372 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1373 | return VERR_NO_MEMORY;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | pMemLnx->Core.u.ResVirt.R0Process = R0Process;
|
---|
1377 | *ppMem = &pMemLnx->Core;
|
---|
1378 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1379 | return VINF_SUCCESS;
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 |
|
---|
1383 | DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap,
|
---|
1384 | void *pvFixed, size_t uAlignment,
|
---|
1385 | unsigned fProt, size_t offSub, size_t cbSub)
|
---|
1386 | {
|
---|
1387 | int rc = VERR_NO_MEMORY;
|
---|
1388 | PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
|
---|
1389 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1390 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
1391 |
|
---|
1392 | /* Fail if requested to do something we can't. */
|
---|
1393 | AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
|
---|
1394 | AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
|
---|
1395 | if (uAlignment > PAGE_SIZE)
|
---|
1396 | return VERR_NOT_SUPPORTED;
|
---|
1397 |
|
---|
1398 | /*
|
---|
1399 | * Create the IPRT memory object.
|
---|
1400 | */
|
---|
1401 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
|
---|
1402 | if (pMemLnx)
|
---|
1403 | {
|
---|
1404 | if (pMemLnxToMap->cPages)
|
---|
1405 | {
|
---|
1406 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 22)
|
---|
1407 | /*
|
---|
1408 | * Use vmap - 2.4.22 and later.
|
---|
1409 | */
|
---|
1410 | pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, true /* kernel */);
|
---|
1411 | # ifdef VM_MAP
|
---|
1412 | pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_MAP, fPg);
|
---|
1413 | # else
|
---|
1414 | pMemLnx->Core.pv = vmap(&pMemLnxToMap->apPages[0], pMemLnxToMap->cPages, VM_ALLOC, fPg);
|
---|
1415 | # endif
|
---|
1416 | if (pMemLnx->Core.pv)
|
---|
1417 | {
|
---|
1418 | pMemLnx->fMappedToRing0 = true;
|
---|
1419 | rc = VINF_SUCCESS;
|
---|
1420 | }
|
---|
1421 | else
|
---|
1422 | rc = VERR_MAP_FAILED;
|
---|
1423 |
|
---|
1424 | #else /* < 2.4.22 */
|
---|
1425 | /*
|
---|
1426 | * Only option here is to share mappings if possible and forget about fProt.
|
---|
1427 | */
|
---|
1428 | if (rtR0MemObjIsRing3(pMemToMap))
|
---|
1429 | rc = VERR_NOT_SUPPORTED;
|
---|
1430 | else
|
---|
1431 | {
|
---|
1432 | rc = VINF_SUCCESS;
|
---|
1433 | if (!pMemLnxToMap->Core.pv)
|
---|
1434 | rc = rtR0MemObjLinuxVMap(pMemLnxToMap, !!(fProt & RTMEM_PROT_EXEC));
|
---|
1435 | if (RT_SUCCESS(rc))
|
---|
1436 | {
|
---|
1437 | Assert(pMemLnxToMap->Core.pv);
|
---|
1438 | pMemLnx->Core.pv = pMemLnxToMap->Core.pv;
|
---|
1439 | }
|
---|
1440 | }
|
---|
1441 | #endif
|
---|
1442 | }
|
---|
1443 | else
|
---|
1444 | {
|
---|
1445 | /*
|
---|
1446 | * MMIO / physical memory.
|
---|
1447 | */
|
---|
1448 | Assert(pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS && !pMemLnxToMap->Core.u.Phys.fAllocated);
|
---|
1449 | pMemLnx->Core.pv = pMemLnxToMap->Core.u.Phys.uCachePolicy == RTMEM_CACHE_POLICY_MMIO
|
---|
1450 | ? ioremap_nocache(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb)
|
---|
1451 | : ioremap(pMemLnxToMap->Core.u.Phys.PhysBase, pMemLnxToMap->Core.cb);
|
---|
1452 | if (pMemLnx->Core.pv)
|
---|
1453 | {
|
---|
1454 | /** @todo fix protection. */
|
---|
1455 | rc = VINF_SUCCESS;
|
---|
1456 | }
|
---|
1457 | }
|
---|
1458 | if (RT_SUCCESS(rc))
|
---|
1459 | {
|
---|
1460 | pMemLnx->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
|
---|
1461 | *ppMem = &pMemLnx->Core;
|
---|
1462 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1463 | return VINF_SUCCESS;
|
---|
1464 | }
|
---|
1465 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1469 | return rc;
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 |
|
---|
1473 | #ifdef VBOX_USE_PAE_HACK
|
---|
1474 | /**
|
---|
1475 | * Replace the PFN of a PTE with the address of the actual page.
|
---|
1476 | *
|
---|
1477 | * The caller maps a reserved dummy page at the address with the desired access
|
---|
1478 | * and flags.
|
---|
1479 | *
|
---|
1480 | * This hack is required for older Linux kernels which don't provide
|
---|
1481 | * remap_pfn_range().
|
---|
1482 | *
|
---|
1483 | * @returns 0 on success, -ENOMEM on failure.
|
---|
1484 | * @param mm The memory context.
|
---|
1485 | * @param ulAddr The mapping address.
|
---|
1486 | * @param Phys The physical address of the page to map.
|
---|
1487 | */
|
---|
1488 | static int rtR0MemObjLinuxFixPte(struct mm_struct *mm, unsigned long ulAddr, RTHCPHYS Phys)
|
---|
1489 | {
|
---|
1490 | int rc = -ENOMEM;
|
---|
1491 | pgd_t *pgd;
|
---|
1492 |
|
---|
1493 | spin_lock(&mm->page_table_lock);
|
---|
1494 |
|
---|
1495 | pgd = pgd_offset(mm, ulAddr);
|
---|
1496 | if (!pgd_none(*pgd) && !pgd_bad(*pgd))
|
---|
1497 | {
|
---|
1498 | pmd_t *pmd = pmd_offset(pgd, ulAddr);
|
---|
1499 | if (!pmd_none(*pmd))
|
---|
1500 | {
|
---|
1501 | pte_t *ptep = pte_offset_map(pmd, ulAddr);
|
---|
1502 | if (ptep)
|
---|
1503 | {
|
---|
1504 | pte_t pte = *ptep;
|
---|
1505 | pte.pte_high &= 0xfff00000;
|
---|
1506 | pte.pte_high |= ((Phys >> 32) & 0x000fffff);
|
---|
1507 | pte.pte_low &= 0x00000fff;
|
---|
1508 | pte.pte_low |= (Phys & 0xfffff000);
|
---|
1509 | set_pte(ptep, pte);
|
---|
1510 | pte_unmap(ptep);
|
---|
1511 | rc = 0;
|
---|
1512 | }
|
---|
1513 | }
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | spin_unlock(&mm->page_table_lock);
|
---|
1517 | return rc;
|
---|
1518 | }
|
---|
1519 | #endif /* VBOX_USE_PAE_HACK */
|
---|
1520 |
|
---|
1521 |
|
---|
1522 | DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed,
|
---|
1523 | size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
|
---|
1524 | {
|
---|
1525 | struct task_struct *pTask = rtR0ProcessToLinuxTask(R0Process);
|
---|
1526 | PRTR0MEMOBJLNX pMemLnxToMap = (PRTR0MEMOBJLNX)pMemToMap;
|
---|
1527 | int rc = VERR_NO_MEMORY;
|
---|
1528 | PRTR0MEMOBJLNX pMemLnx;
|
---|
1529 | #ifdef VBOX_USE_PAE_HACK
|
---|
1530 | struct page *pDummyPage;
|
---|
1531 | RTHCPHYS DummyPhys;
|
---|
1532 | #endif
|
---|
1533 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
1534 |
|
---|
1535 | /*
|
---|
1536 | * Check for restrictions.
|
---|
1537 | */
|
---|
1538 | if (!pTask)
|
---|
1539 | return VERR_NOT_SUPPORTED;
|
---|
1540 | if (uAlignment > PAGE_SIZE)
|
---|
1541 | return VERR_NOT_SUPPORTED;
|
---|
1542 |
|
---|
1543 | #ifdef VBOX_USE_PAE_HACK
|
---|
1544 | /*
|
---|
1545 | * Allocate a dummy page for use when mapping the memory.
|
---|
1546 | */
|
---|
1547 | pDummyPage = alloc_page(GFP_USER | __GFP_NOWARN);
|
---|
1548 | if (!pDummyPage)
|
---|
1549 | {
|
---|
1550 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1551 | return VERR_NO_MEMORY;
|
---|
1552 | }
|
---|
1553 | SetPageReserved(pDummyPage);
|
---|
1554 | DummyPhys = page_to_phys(pDummyPage);
|
---|
1555 | #endif
|
---|
1556 |
|
---|
1557 | /*
|
---|
1558 | * Create the IPRT memory object.
|
---|
1559 | */
|
---|
1560 | pMemLnx = (PRTR0MEMOBJLNX)rtR0MemObjNew(sizeof(*pMemLnx), RTR0MEMOBJTYPE_MAPPING, NULL, pMemLnxToMap->Core.cb);
|
---|
1561 | if (pMemLnx)
|
---|
1562 | {
|
---|
1563 | /*
|
---|
1564 | * Allocate user space mapping.
|
---|
1565 | */
|
---|
1566 | void *pv;
|
---|
1567 | pv = rtR0MemObjLinuxDoMmap(R3PtrFixed, pMemLnxToMap->Core.cb, uAlignment, pTask, fProt);
|
---|
1568 | if (pv != (void *)-1)
|
---|
1569 | {
|
---|
1570 | /*
|
---|
1571 | * Map page by page into the mmap area.
|
---|
1572 | * This is generic, paranoid and not very efficient.
|
---|
1573 | */
|
---|
1574 | pgprot_t fPg = rtR0MemObjLinuxConvertProt(fProt, false /* user */);
|
---|
1575 | unsigned long ulAddrCur = (unsigned long)pv;
|
---|
1576 | const size_t cPages = pMemLnxToMap->Core.cb >> PAGE_SHIFT;
|
---|
1577 | size_t iPage;
|
---|
1578 |
|
---|
1579 | down_write(&pTask->mm->mmap_sem);
|
---|
1580 |
|
---|
1581 | rc = VINF_SUCCESS;
|
---|
1582 | if (pMemLnxToMap->cPages)
|
---|
1583 | {
|
---|
1584 | for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE)
|
---|
1585 | {
|
---|
1586 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
|
---|
1587 | RTHCPHYS Phys = page_to_phys(pMemLnxToMap->apPages[iPage]);
|
---|
1588 | #endif
|
---|
1589 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1590 | struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
|
---|
1591 | AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
|
---|
1592 | #endif
|
---|
1593 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_X86)
|
---|
1594 | /* remap_page_range() limitation on x86 */
|
---|
1595 | AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
|
---|
1596 | #endif
|
---|
1597 |
|
---|
1598 | #if defined(VBOX_USE_INSERT_PAGE) && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
|
---|
1599 | rc = vm_insert_page(vma, ulAddrCur, pMemLnxToMap->apPages[iPage]);
|
---|
1600 | /* Thes flags help making 100% sure some bad stuff wont happen (swap, core, ++).
|
---|
1601 | * See remap_pfn_range() in mm/memory.c */
|
---|
1602 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
|
---|
1603 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
|
---|
1604 | #else
|
---|
1605 | vma->vm_flags |= VM_RESERVED;
|
---|
1606 | #endif
|
---|
1607 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
|
---|
1608 | rc = remap_pfn_range(vma, ulAddrCur, page_to_pfn(pMemLnxToMap->apPages[iPage]), PAGE_SIZE, fPg);
|
---|
1609 | #elif defined(VBOX_USE_PAE_HACK)
|
---|
1610 | rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
|
---|
1611 | if (!rc)
|
---|
1612 | rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
|
---|
1613 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1614 | rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1615 | #else /* 2.4 */
|
---|
1616 | rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1617 | #endif
|
---|
1618 | if (rc)
|
---|
1619 | {
|
---|
1620 | rc = VERR_NO_MEMORY;
|
---|
1621 | break;
|
---|
1622 | }
|
---|
1623 | }
|
---|
1624 | }
|
---|
1625 | else
|
---|
1626 | {
|
---|
1627 | RTHCPHYS Phys;
|
---|
1628 | if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS)
|
---|
1629 | Phys = pMemLnxToMap->Core.u.Phys.PhysBase;
|
---|
1630 | else if (pMemLnxToMap->Core.enmType == RTR0MEMOBJTYPE_CONT)
|
---|
1631 | Phys = pMemLnxToMap->Core.u.Cont.Phys;
|
---|
1632 | else
|
---|
1633 | {
|
---|
1634 | AssertMsgFailed(("%d\n", pMemLnxToMap->Core.enmType));
|
---|
1635 | Phys = NIL_RTHCPHYS;
|
---|
1636 | }
|
---|
1637 | if (Phys != NIL_RTHCPHYS)
|
---|
1638 | {
|
---|
1639 | for (iPage = 0; iPage < cPages; iPage++, ulAddrCur += PAGE_SIZE, Phys += PAGE_SIZE)
|
---|
1640 | {
|
---|
1641 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1642 | struct vm_area_struct *vma = find_vma(pTask->mm, ulAddrCur); /* this is probably the same for all the pages... */
|
---|
1643 | AssertBreakStmt(vma, rc = VERR_INTERNAL_ERROR);
|
---|
1644 | #endif
|
---|
1645 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) && defined(RT_ARCH_X86)
|
---|
1646 | /* remap_page_range() limitation on x86 */
|
---|
1647 | AssertBreakStmt(Phys < _4G, rc = VERR_NO_MEMORY);
|
---|
1648 | #endif
|
---|
1649 |
|
---|
1650 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
|
---|
1651 | rc = remap_pfn_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1652 | #elif defined(VBOX_USE_PAE_HACK)
|
---|
1653 | rc = remap_page_range(vma, ulAddrCur, DummyPhys, PAGE_SIZE, fPg);
|
---|
1654 | if (!rc)
|
---|
1655 | rc = rtR0MemObjLinuxFixPte(pTask->mm, ulAddrCur, Phys);
|
---|
1656 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(HAVE_26_STYLE_REMAP_PAGE_RANGE)
|
---|
1657 | rc = remap_page_range(vma, ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1658 | #else /* 2.4 */
|
---|
1659 | rc = remap_page_range(ulAddrCur, Phys, PAGE_SIZE, fPg);
|
---|
1660 | #endif
|
---|
1661 | if (rc)
|
---|
1662 | {
|
---|
1663 | rc = VERR_NO_MEMORY;
|
---|
1664 | break;
|
---|
1665 | }
|
---|
1666 | }
|
---|
1667 | }
|
---|
1668 | }
|
---|
1669 |
|
---|
1670 | #ifdef CONFIG_NUMA_BALANCING
|
---|
1671 | # if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
|
---|
1672 | # ifdef RHEL_RELEASE_CODE
|
---|
1673 | # if RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(7, 0)
|
---|
1674 | # define VBOX_NUMA_HACK_OLD
|
---|
1675 | # endif
|
---|
1676 | # endif
|
---|
1677 | # endif
|
---|
1678 | if (RT_SUCCESS(rc))
|
---|
1679 | {
|
---|
1680 | /** @todo Ugly hack! But right now we have no other means to
|
---|
1681 | * disable automatic NUMA page balancing. */
|
---|
1682 | # ifdef RT_OS_X86
|
---|
1683 | # ifdef VBOX_NUMA_HACK_OLD
|
---|
1684 | pTask->mm->numa_next_reset = jiffies + 0x7fffffffUL;
|
---|
1685 | # endif
|
---|
1686 | pTask->mm->numa_next_scan = jiffies + 0x7fffffffUL;
|
---|
1687 | # else
|
---|
1688 | # ifdef VBOX_NUMA_HACK_OLD
|
---|
1689 | pTask->mm->numa_next_reset = jiffies + 0x7fffffffffffffffUL;
|
---|
1690 | # endif
|
---|
1691 | pTask->mm->numa_next_scan = jiffies + 0x7fffffffffffffffUL;
|
---|
1692 | # endif
|
---|
1693 | }
|
---|
1694 | #endif /* CONFIG_NUMA_BALANCING */
|
---|
1695 |
|
---|
1696 | up_write(&pTask->mm->mmap_sem);
|
---|
1697 |
|
---|
1698 | if (RT_SUCCESS(rc))
|
---|
1699 | {
|
---|
1700 | #ifdef VBOX_USE_PAE_HACK
|
---|
1701 | __free_page(pDummyPage);
|
---|
1702 | #endif
|
---|
1703 | pMemLnx->Core.pv = pv;
|
---|
1704 | pMemLnx->Core.u.Mapping.R0Process = R0Process;
|
---|
1705 | *ppMem = &pMemLnx->Core;
|
---|
1706 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1707 | return VINF_SUCCESS;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | /*
|
---|
1711 | * Bail out.
|
---|
1712 | */
|
---|
1713 | rtR0MemObjLinuxDoMunmap(pv, pMemLnxToMap->Core.cb, pTask);
|
---|
1714 | }
|
---|
1715 | rtR0MemObjDelete(&pMemLnx->Core);
|
---|
1716 | }
|
---|
1717 | #ifdef VBOX_USE_PAE_HACK
|
---|
1718 | __free_page(pDummyPage);
|
---|
1719 | #endif
|
---|
1720 |
|
---|
1721 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
1722 | return rc;
|
---|
1723 | }
|
---|
1724 |
|
---|
1725 |
|
---|
1726 | DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
|
---|
1727 | {
|
---|
1728 | NOREF(pMem);
|
---|
1729 | NOREF(offSub);
|
---|
1730 | NOREF(cbSub);
|
---|
1731 | NOREF(fProt);
|
---|
1732 | return VERR_NOT_SUPPORTED;
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 |
|
---|
1736 | DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
|
---|
1737 | {
|
---|
1738 | PRTR0MEMOBJLNX pMemLnx = (PRTR0MEMOBJLNX)pMem;
|
---|
1739 |
|
---|
1740 | if (pMemLnx->cPages)
|
---|
1741 | return page_to_phys(pMemLnx->apPages[iPage]);
|
---|
1742 |
|
---|
1743 | switch (pMemLnx->Core.enmType)
|
---|
1744 | {
|
---|
1745 | case RTR0MEMOBJTYPE_CONT:
|
---|
1746 | return pMemLnx->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
|
---|
1747 |
|
---|
1748 | case RTR0MEMOBJTYPE_PHYS:
|
---|
1749 | return pMemLnx->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
|
---|
1750 |
|
---|
1751 | /* the parent knows */
|
---|
1752 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
1753 | return rtR0MemObjNativeGetPagePhysAddr(pMemLnx->Core.uRel.Child.pParent, iPage);
|
---|
1754 |
|
---|
1755 | /* cPages > 0 */
|
---|
1756 | case RTR0MEMOBJTYPE_LOW:
|
---|
1757 | case RTR0MEMOBJTYPE_LOCK:
|
---|
1758 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
1759 | case RTR0MEMOBJTYPE_PAGE:
|
---|
1760 | default:
|
---|
1761 | AssertMsgFailed(("%d\n", pMemLnx->Core.enmType));
|
---|
1762 | /* fall thru */
|
---|
1763 |
|
---|
1764 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
1765 | return NIL_RTHCPHYS;
|
---|
1766 | }
|
---|
1767 | }
|
---|
1768 |
|
---|