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