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