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