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