1 | /* $Id: memobj-r0drv-nt.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Ring-0 Memory Objects, NT.
|
---|
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-nt-kernel.h"
|
---|
32 |
|
---|
33 | #include <iprt/memobj.h>
|
---|
34 | #include <iprt/alloc.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/log.h>
|
---|
38 | #include <iprt/param.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/process.h>
|
---|
41 | #include "internal/memobj.h"
|
---|
42 | #include "internal-r0drv-nt.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Defined Constants And Macros *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | /** Maximum number of bytes we try to lock down in one go.
|
---|
49 | * This is supposed to have a limit right below 256MB, but this appears
|
---|
50 | * to actually be much lower. The values here have been determined experimentally.
|
---|
51 | */
|
---|
52 | #ifdef RT_ARCH_X86
|
---|
53 | # define MAX_LOCK_MEM_SIZE (32*1024*1024) /* 32MB */
|
---|
54 | #endif
|
---|
55 | #ifdef RT_ARCH_AMD64
|
---|
56 | # define MAX_LOCK_MEM_SIZE (24*1024*1024) /* 24MB */
|
---|
57 | #endif
|
---|
58 |
|
---|
59 |
|
---|
60 | /*********************************************************************************************************************************
|
---|
61 | * Structures and Typedefs *
|
---|
62 | *********************************************************************************************************************************/
|
---|
63 | /**
|
---|
64 | * The NT version of the memory object structure.
|
---|
65 | */
|
---|
66 | typedef struct RTR0MEMOBJNT
|
---|
67 | {
|
---|
68 | /** The core structure. */
|
---|
69 | RTR0MEMOBJINTERNAL Core;
|
---|
70 | /** Used MmAllocatePagesForMdl(). */
|
---|
71 | bool fAllocatedPagesForMdl;
|
---|
72 | /** Set if this is sub-section of the parent. */
|
---|
73 | bool fSubMapping;
|
---|
74 | /** Pointer returned by MmSecureVirtualMemory */
|
---|
75 | PVOID pvSecureMem;
|
---|
76 | /** The number of PMDLs (memory descriptor lists) in the array. */
|
---|
77 | uint32_t cMdls;
|
---|
78 | /** Array of MDL pointers. (variable size) */
|
---|
79 | PMDL apMdls[1];
|
---|
80 | } RTR0MEMOBJNT;
|
---|
81 | /** Pointer to the NT version of the memory object structure. */
|
---|
82 | typedef RTR0MEMOBJNT *PRTR0MEMOBJNT;
|
---|
83 |
|
---|
84 |
|
---|
85 |
|
---|
86 | DECLHIDDEN(int) rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
|
---|
87 | {
|
---|
88 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)pMem;
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Deal with it on a per type basis (just as a variation).
|
---|
92 | */
|
---|
93 | switch (pMemNt->Core.enmType)
|
---|
94 | {
|
---|
95 | case RTR0MEMOBJTYPE_LOW:
|
---|
96 | if (pMemNt->fAllocatedPagesForMdl)
|
---|
97 | {
|
---|
98 | Assert(pMemNt->Core.pv && pMemNt->cMdls == 1 && pMemNt->apMdls[0]);
|
---|
99 | MmUnmapLockedPages(pMemNt->Core.pv, pMemNt->apMdls[0]);
|
---|
100 | pMemNt->Core.pv = NULL;
|
---|
101 | if (pMemNt->pvSecureMem)
|
---|
102 | {
|
---|
103 | g_pfnrtMmUnsecureVirtualMemory(pMemNt->pvSecureMem);
|
---|
104 | pMemNt->pvSecureMem = NULL;
|
---|
105 | }
|
---|
106 |
|
---|
107 | g_pfnrtMmFreePagesFromMdl(pMemNt->apMdls[0]);
|
---|
108 | ExFreePool(pMemNt->apMdls[0]);
|
---|
109 | pMemNt->apMdls[0] = NULL;
|
---|
110 | pMemNt->cMdls = 0;
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | AssertFailed();
|
---|
114 | break;
|
---|
115 |
|
---|
116 | case RTR0MEMOBJTYPE_PAGE:
|
---|
117 | Assert(pMemNt->Core.pv);
|
---|
118 | if (pMemNt->fAllocatedPagesForMdl)
|
---|
119 | {
|
---|
120 | Assert(pMemNt->Core.pv && pMemNt->cMdls == 1 && pMemNt->apMdls[0]);
|
---|
121 | Assert(pMemNt->pvSecureMem == NULL);
|
---|
122 | MmUnmapLockedPages(pMemNt->Core.pv, pMemNt->apMdls[0]);
|
---|
123 | g_pfnrtMmFreePagesFromMdl(pMemNt->apMdls[0]);
|
---|
124 | ExFreePool(pMemNt->apMdls[0]);
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | if (g_pfnrtExFreePoolWithTag)
|
---|
129 | g_pfnrtExFreePoolWithTag(pMemNt->Core.pv, IPRT_NT_POOL_TAG);
|
---|
130 | else
|
---|
131 | ExFreePool(pMemNt->Core.pv);
|
---|
132 |
|
---|
133 | Assert(pMemNt->cMdls == 1 && pMemNt->apMdls[0]);
|
---|
134 | IoFreeMdl(pMemNt->apMdls[0]);
|
---|
135 | }
|
---|
136 | pMemNt->Core.pv = NULL;
|
---|
137 | pMemNt->apMdls[0] = NULL;
|
---|
138 | pMemNt->cMdls = 0;
|
---|
139 | break;
|
---|
140 |
|
---|
141 | case RTR0MEMOBJTYPE_CONT:
|
---|
142 | Assert(pMemNt->Core.pv);
|
---|
143 | MmFreeContiguousMemory(pMemNt->Core.pv);
|
---|
144 | pMemNt->Core.pv = NULL;
|
---|
145 |
|
---|
146 | Assert(pMemNt->cMdls == 1 && pMemNt->apMdls[0]);
|
---|
147 | IoFreeMdl(pMemNt->apMdls[0]);
|
---|
148 | pMemNt->apMdls[0] = NULL;
|
---|
149 | pMemNt->cMdls = 0;
|
---|
150 | break;
|
---|
151 |
|
---|
152 | case RTR0MEMOBJTYPE_PHYS:
|
---|
153 | /* rtR0MemObjNativeEnterPhys? */
|
---|
154 | if (!pMemNt->Core.u.Phys.fAllocated)
|
---|
155 | {
|
---|
156 | Assert(!pMemNt->fAllocatedPagesForMdl);
|
---|
157 | /* Nothing to do here. */
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | RT_FALL_THRU();
|
---|
161 |
|
---|
162 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
163 | if (pMemNt->fAllocatedPagesForMdl)
|
---|
164 | {
|
---|
165 | g_pfnrtMmFreePagesFromMdl(pMemNt->apMdls[0]);
|
---|
166 | ExFreePool(pMemNt->apMdls[0]);
|
---|
167 | pMemNt->apMdls[0] = NULL;
|
---|
168 | pMemNt->cMdls = 0;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | AssertFailed();
|
---|
172 | break;
|
---|
173 |
|
---|
174 | case RTR0MEMOBJTYPE_LOCK:
|
---|
175 | if (pMemNt->pvSecureMem)
|
---|
176 | {
|
---|
177 | g_pfnrtMmUnsecureVirtualMemory(pMemNt->pvSecureMem);
|
---|
178 | pMemNt->pvSecureMem = NULL;
|
---|
179 | }
|
---|
180 | for (uint32_t i = 0; i < pMemNt->cMdls; i++)
|
---|
181 | {
|
---|
182 | MmUnlockPages(pMemNt->apMdls[i]);
|
---|
183 | IoFreeMdl(pMemNt->apMdls[i]);
|
---|
184 | pMemNt->apMdls[i] = NULL;
|
---|
185 | }
|
---|
186 | break;
|
---|
187 |
|
---|
188 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
189 | /* if (pMemNt->Core.u.ResVirt.R0Process == NIL_RTR0PROCESS)
|
---|
190 | {
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | }*/
|
---|
195 | AssertMsgFailed(("RTR0MEMOBJTYPE_RES_VIRT\n"));
|
---|
196 | return VERR_INTERNAL_ERROR;
|
---|
197 | break;
|
---|
198 |
|
---|
199 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
200 | {
|
---|
201 | PRTR0MEMOBJNT pMemNtParent = (PRTR0MEMOBJNT)pMemNt->Core.uRel.Child.pParent;
|
---|
202 | Assert(pMemNtParent);
|
---|
203 | Assert(pMemNt->Core.pv);
|
---|
204 | Assert((pMemNt->cMdls == 0 && !pMemNt->fSubMapping) || (pMemNt->cMdls == 1 && pMemNt->fSubMapping));
|
---|
205 | if (pMemNtParent->cMdls)
|
---|
206 | {
|
---|
207 | Assert(pMemNtParent->cMdls == 1 && pMemNtParent->apMdls[0]);
|
---|
208 | Assert( pMemNt->Core.u.Mapping.R0Process == NIL_RTR0PROCESS
|
---|
209 | || pMemNt->Core.u.Mapping.R0Process == RTR0ProcHandleSelf());
|
---|
210 | if (!pMemNt->cMdls)
|
---|
211 | MmUnmapLockedPages(pMemNt->Core.pv, pMemNtParent->apMdls[0]);
|
---|
212 | else
|
---|
213 | {
|
---|
214 | MmUnmapLockedPages(pMemNt->Core.pv, pMemNt->apMdls[0]);
|
---|
215 | IoFreeMdl(pMemNt->apMdls[0]);
|
---|
216 | pMemNt->apMdls[0] = NULL;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | Assert( pMemNtParent->Core.enmType == RTR0MEMOBJTYPE_PHYS
|
---|
222 | && !pMemNtParent->Core.u.Phys.fAllocated);
|
---|
223 | Assert(pMemNt->Core.u.Mapping.R0Process == NIL_RTR0PROCESS);
|
---|
224 | Assert(!pMemNt->fSubMapping);
|
---|
225 | MmUnmapIoSpace(pMemNt->Core.pv, pMemNt->Core.cb);
|
---|
226 | }
|
---|
227 | pMemNt->Core.pv = NULL;
|
---|
228 | break;
|
---|
229 | }
|
---|
230 |
|
---|
231 | default:
|
---|
232 | AssertMsgFailed(("enmType=%d\n", pMemNt->Core.enmType));
|
---|
233 | return VERR_INTERNAL_ERROR;
|
---|
234 | }
|
---|
235 |
|
---|
236 | return VINF_SUCCESS;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
241 | {
|
---|
242 | AssertMsgReturn(cb <= _1G, ("%#x\n", cb), VERR_OUT_OF_RANGE); /* for safe size_t -> ULONG */
|
---|
243 | RT_NOREF1(fExecutable);
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * Use MmAllocatePagesForMdl if the allocation is a little bit big.
|
---|
247 | */
|
---|
248 | int rc = VERR_NO_PAGE_MEMORY;
|
---|
249 | if ( cb > _1M
|
---|
250 | && g_pfnrtMmAllocatePagesForMdl
|
---|
251 | && g_pfnrtMmFreePagesFromMdl
|
---|
252 | && g_pfnrtMmMapLockedPagesSpecifyCache)
|
---|
253 | {
|
---|
254 | PHYSICAL_ADDRESS Zero;
|
---|
255 | Zero.QuadPart = 0;
|
---|
256 | PHYSICAL_ADDRESS HighAddr;
|
---|
257 | HighAddr.QuadPart = MAXLONGLONG;
|
---|
258 | PMDL pMdl = g_pfnrtMmAllocatePagesForMdl(Zero, HighAddr, Zero, cb);
|
---|
259 | if (pMdl)
|
---|
260 | {
|
---|
261 | if (MmGetMdlByteCount(pMdl) >= cb)
|
---|
262 | {
|
---|
263 | __try
|
---|
264 | {
|
---|
265 | void *pv = g_pfnrtMmMapLockedPagesSpecifyCache(pMdl, KernelMode, MmCached, NULL /* no base address */,
|
---|
266 | FALSE /* no bug check on failure */, NormalPagePriority);
|
---|
267 | if (pv)
|
---|
268 | {
|
---|
269 | #ifdef RT_ARCH_AMD64
|
---|
270 | if (fExecutable)
|
---|
271 | MmProtectMdlSystemAddress(pMdl, PAGE_EXECUTE_READWRITE);
|
---|
272 | #endif
|
---|
273 |
|
---|
274 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_PAGE, pv, cb);
|
---|
275 | if (pMemNt)
|
---|
276 | {
|
---|
277 | pMemNt->fAllocatedPagesForMdl = true;
|
---|
278 | pMemNt->cMdls = 1;
|
---|
279 | pMemNt->apMdls[0] = pMdl;
|
---|
280 | *ppMem = &pMemNt->Core;
|
---|
281 | return VINF_SUCCESS;
|
---|
282 | }
|
---|
283 | MmUnmapLockedPages(pv, pMdl);
|
---|
284 | }
|
---|
285 | }
|
---|
286 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
287 | {
|
---|
288 | # ifdef LOG_ENABLED
|
---|
289 | NTSTATUS rcNt = GetExceptionCode();
|
---|
290 | Log(("rtR0MemObjNativeAllocLow: Exception Code %#x\n", rcNt));
|
---|
291 | # endif
|
---|
292 | /* nothing */
|
---|
293 | }
|
---|
294 | }
|
---|
295 | g_pfnrtMmFreePagesFromMdl(pMdl);
|
---|
296 | ExFreePool(pMdl);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * Try allocate the memory and create an MDL for them so
|
---|
302 | * we can query the physical addresses and do mappings later
|
---|
303 | * without running into out-of-memory conditions and similar problems.
|
---|
304 | */
|
---|
305 | void *pv;
|
---|
306 | if (g_pfnrtExAllocatePoolWithTag)
|
---|
307 | pv = g_pfnrtExAllocatePoolWithTag(NonPagedPool, cb, IPRT_NT_POOL_TAG);
|
---|
308 | else
|
---|
309 | pv = ExAllocatePool(NonPagedPool, cb);
|
---|
310 | if (pv)
|
---|
311 | {
|
---|
312 | PMDL pMdl = IoAllocateMdl(pv, (ULONG)cb, FALSE, FALSE, NULL);
|
---|
313 | if (pMdl)
|
---|
314 | {
|
---|
315 | MmBuildMdlForNonPagedPool(pMdl);
|
---|
316 | #ifdef RT_ARCH_AMD64
|
---|
317 | if (fExecutable)
|
---|
318 | MmProtectMdlSystemAddress(pMdl, PAGE_EXECUTE_READWRITE);
|
---|
319 | #endif
|
---|
320 |
|
---|
321 | /*
|
---|
322 | * Create the IPRT memory object.
|
---|
323 | */
|
---|
324 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_PAGE, pv, cb);
|
---|
325 | if (pMemNt)
|
---|
326 | {
|
---|
327 | pMemNt->cMdls = 1;
|
---|
328 | pMemNt->apMdls[0] = pMdl;
|
---|
329 | *ppMem = &pMemNt->Core;
|
---|
330 | return VINF_SUCCESS;
|
---|
331 | }
|
---|
332 |
|
---|
333 | rc = VERR_NO_MEMORY;
|
---|
334 | IoFreeMdl(pMdl);
|
---|
335 | }
|
---|
336 | ExFreePool(pv);
|
---|
337 | }
|
---|
338 | return rc;
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
343 | {
|
---|
344 | AssertMsgReturn(cb <= _1G, ("%#x\n", cb), VERR_OUT_OF_RANGE); /* for safe size_t -> ULONG */
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Try see if we get lucky first...
|
---|
348 | * (We could probably just assume we're lucky on NT4.)
|
---|
349 | */
|
---|
350 | int rc = rtR0MemObjNativeAllocPage(ppMem, cb, fExecutable);
|
---|
351 | if (RT_SUCCESS(rc))
|
---|
352 | {
|
---|
353 | size_t iPage = cb >> PAGE_SHIFT;
|
---|
354 | while (iPage-- > 0)
|
---|
355 | if (rtR0MemObjNativeGetPagePhysAddr(*ppMem, iPage) >= _4G)
|
---|
356 | {
|
---|
357 | rc = VERR_NO_LOW_MEMORY;
|
---|
358 | break;
|
---|
359 | }
|
---|
360 | if (RT_SUCCESS(rc))
|
---|
361 | return rc;
|
---|
362 |
|
---|
363 | /* The following ASSUMES that rtR0MemObjNativeAllocPage returns a completed object. */
|
---|
364 | RTR0MemObjFree(*ppMem, false);
|
---|
365 | *ppMem = NULL;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /*
|
---|
369 | * Use MmAllocatePagesForMdl to specify the range of physical addresses we wish to use.
|
---|
370 | */
|
---|
371 | if ( g_pfnrtMmAllocatePagesForMdl
|
---|
372 | && g_pfnrtMmFreePagesFromMdl
|
---|
373 | && g_pfnrtMmMapLockedPagesSpecifyCache)
|
---|
374 | {
|
---|
375 | PHYSICAL_ADDRESS Zero;
|
---|
376 | Zero.QuadPart = 0;
|
---|
377 | PHYSICAL_ADDRESS HighAddr;
|
---|
378 | HighAddr.QuadPart = _4G - 1;
|
---|
379 | PMDL pMdl = g_pfnrtMmAllocatePagesForMdl(Zero, HighAddr, Zero, cb);
|
---|
380 | if (pMdl)
|
---|
381 | {
|
---|
382 | if (MmGetMdlByteCount(pMdl) >= cb)
|
---|
383 | {
|
---|
384 | __try
|
---|
385 | {
|
---|
386 | void *pv = g_pfnrtMmMapLockedPagesSpecifyCache(pMdl, KernelMode, MmCached, NULL /* no base address */,
|
---|
387 | FALSE /* no bug check on failure */, NormalPagePriority);
|
---|
388 | if (pv)
|
---|
389 | {
|
---|
390 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_LOW, pv, cb);
|
---|
391 | if (pMemNt)
|
---|
392 | {
|
---|
393 | pMemNt->fAllocatedPagesForMdl = true;
|
---|
394 | pMemNt->cMdls = 1;
|
---|
395 | pMemNt->apMdls[0] = pMdl;
|
---|
396 | *ppMem = &pMemNt->Core;
|
---|
397 | return VINF_SUCCESS;
|
---|
398 | }
|
---|
399 | MmUnmapLockedPages(pv, pMdl);
|
---|
400 | }
|
---|
401 | }
|
---|
402 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
403 | {
|
---|
404 | # ifdef LOG_ENABLED
|
---|
405 | NTSTATUS rcNt = GetExceptionCode();
|
---|
406 | Log(("rtR0MemObjNativeAllocLow: Exception Code %#x\n", rcNt));
|
---|
407 | # endif
|
---|
408 | /* nothing */
|
---|
409 | }
|
---|
410 | }
|
---|
411 | g_pfnrtMmFreePagesFromMdl(pMdl);
|
---|
412 | ExFreePool(pMdl);
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | /*
|
---|
417 | * Fall back on contiguous memory...
|
---|
418 | */
|
---|
419 | return rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * Internal worker for rtR0MemObjNativeAllocCont(), rtR0MemObjNativeAllocPhys()
|
---|
425 | * and rtR0MemObjNativeAllocPhysNC() that takes a max physical address in addition
|
---|
426 | * to what rtR0MemObjNativeAllocCont() does.
|
---|
427 | *
|
---|
428 | * @returns IPRT status code.
|
---|
429 | * @param ppMem Where to store the pointer to the ring-0 memory object.
|
---|
430 | * @param cb The size.
|
---|
431 | * @param fExecutable Whether the mapping should be executable or not.
|
---|
432 | * @param PhysHighest The highest physical address for the pages in allocation.
|
---|
433 | * @param uAlignment The alignment of the physical memory to allocate.
|
---|
434 | * Supported values are PAGE_SIZE, _2M, _4M and _1G.
|
---|
435 | */
|
---|
436 | static int rtR0MemObjNativeAllocContEx(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable, RTHCPHYS PhysHighest,
|
---|
437 | size_t uAlignment)
|
---|
438 | {
|
---|
439 | AssertMsgReturn(cb <= _1G, ("%#x\n", cb), VERR_OUT_OF_RANGE); /* for safe size_t -> ULONG */
|
---|
440 | RT_NOREF1(fExecutable);
|
---|
441 |
|
---|
442 | /*
|
---|
443 | * Allocate the memory and create an MDL for it.
|
---|
444 | */
|
---|
445 | PHYSICAL_ADDRESS PhysAddrHighest;
|
---|
446 | PhysAddrHighest.QuadPart = PhysHighest;
|
---|
447 | void *pv;
|
---|
448 | if (g_pfnrtMmAllocateContiguousMemorySpecifyCache)
|
---|
449 | {
|
---|
450 | PHYSICAL_ADDRESS PhysAddrLowest, PhysAddrBoundary;
|
---|
451 | PhysAddrLowest.QuadPart = 0;
|
---|
452 | PhysAddrBoundary.QuadPart = (uAlignment == PAGE_SIZE) ? 0 : uAlignment;
|
---|
453 | pv = g_pfnrtMmAllocateContiguousMemorySpecifyCache(cb, PhysAddrLowest, PhysAddrHighest, PhysAddrBoundary, MmCached);
|
---|
454 | }
|
---|
455 | else if (uAlignment == PAGE_SIZE)
|
---|
456 | pv = MmAllocateContiguousMemory(cb, PhysAddrHighest);
|
---|
457 | else
|
---|
458 | return VERR_NOT_SUPPORTED;
|
---|
459 | if (!pv)
|
---|
460 | return VERR_NO_MEMORY;
|
---|
461 |
|
---|
462 | PMDL pMdl = IoAllocateMdl(pv, (ULONG)cb, FALSE, FALSE, NULL);
|
---|
463 | if (pMdl)
|
---|
464 | {
|
---|
465 | MmBuildMdlForNonPagedPool(pMdl);
|
---|
466 | #ifdef RT_ARCH_AMD64
|
---|
467 | if (fExecutable)
|
---|
468 | MmProtectMdlSystemAddress(pMdl, PAGE_EXECUTE_READWRITE);
|
---|
469 | #endif
|
---|
470 |
|
---|
471 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_CONT, pv, cb);
|
---|
472 | if (pMemNt)
|
---|
473 | {
|
---|
474 | pMemNt->Core.u.Cont.Phys = (RTHCPHYS)*MmGetMdlPfnArray(pMdl) << PAGE_SHIFT;
|
---|
475 | pMemNt->cMdls = 1;
|
---|
476 | pMemNt->apMdls[0] = pMdl;
|
---|
477 | *ppMem = &pMemNt->Core;
|
---|
478 | return VINF_SUCCESS;
|
---|
479 | }
|
---|
480 |
|
---|
481 | IoFreeMdl(pMdl);
|
---|
482 | }
|
---|
483 | MmFreeContiguousMemory(pv);
|
---|
484 | return VERR_NO_MEMORY;
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
489 | {
|
---|
490 | return rtR0MemObjNativeAllocContEx(ppMem, cb, fExecutable, _4G-1, PAGE_SIZE /* alignment */);
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
|
---|
495 | {
|
---|
496 | /*
|
---|
497 | * Try and see if we're lucky and get a contiguous chunk from MmAllocatePagesForMdl.
|
---|
498 | *
|
---|
499 | * This is preferable to using MmAllocateContiguousMemory because there are
|
---|
500 | * a few situations where the memory shouldn't be mapped, like for instance
|
---|
501 | * VT-x control memory. Since these are rather small allocations (one or
|
---|
502 | * two pages) MmAllocatePagesForMdl will probably be able to satisfy the
|
---|
503 | * request.
|
---|
504 | *
|
---|
505 | * If the allocation is big, the chances are *probably* not very good. The
|
---|
506 | * current limit is kind of random...
|
---|
507 | */
|
---|
508 | if ( cb < _128K
|
---|
509 | && uAlignment == PAGE_SIZE
|
---|
510 | && g_pfnrtMmAllocatePagesForMdl
|
---|
511 | && g_pfnrtMmFreePagesFromMdl)
|
---|
512 | {
|
---|
513 | PHYSICAL_ADDRESS Zero;
|
---|
514 | Zero.QuadPart = 0;
|
---|
515 | PHYSICAL_ADDRESS HighAddr;
|
---|
516 | HighAddr.QuadPart = PhysHighest == NIL_RTHCPHYS ? MAXLONGLONG : PhysHighest;
|
---|
517 | PMDL pMdl = g_pfnrtMmAllocatePagesForMdl(Zero, HighAddr, Zero, cb);
|
---|
518 | if (pMdl)
|
---|
519 | {
|
---|
520 | if (MmGetMdlByteCount(pMdl) >= cb)
|
---|
521 | {
|
---|
522 | PPFN_NUMBER paPfns = MmGetMdlPfnArray(pMdl);
|
---|
523 | PFN_NUMBER Pfn = paPfns[0] + 1;
|
---|
524 | const size_t cPages = cb >> PAGE_SHIFT;
|
---|
525 | size_t iPage;
|
---|
526 | for (iPage = 1; iPage < cPages; iPage++, Pfn++)
|
---|
527 | if (paPfns[iPage] != Pfn)
|
---|
528 | break;
|
---|
529 | if (iPage >= cPages)
|
---|
530 | {
|
---|
531 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
532 | if (pMemNt)
|
---|
533 | {
|
---|
534 | pMemNt->Core.u.Phys.fAllocated = true;
|
---|
535 | pMemNt->Core.u.Phys.PhysBase = (RTHCPHYS)paPfns[0] << PAGE_SHIFT;
|
---|
536 | pMemNt->fAllocatedPagesForMdl = true;
|
---|
537 | pMemNt->cMdls = 1;
|
---|
538 | pMemNt->apMdls[0] = pMdl;
|
---|
539 | *ppMem = &pMemNt->Core;
|
---|
540 | return VINF_SUCCESS;
|
---|
541 | }
|
---|
542 | }
|
---|
543 | }
|
---|
544 | g_pfnrtMmFreePagesFromMdl(pMdl);
|
---|
545 | ExFreePool(pMdl);
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 | return rtR0MemObjNativeAllocContEx(ppMem, cb, false, PhysHighest, uAlignment);
|
---|
550 | }
|
---|
551 |
|
---|
552 |
|
---|
553 | DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
554 | {
|
---|
555 | if (g_pfnrtMmAllocatePagesForMdl && g_pfnrtMmFreePagesFromMdl)
|
---|
556 | {
|
---|
557 | PHYSICAL_ADDRESS Zero;
|
---|
558 | Zero.QuadPart = 0;
|
---|
559 | PHYSICAL_ADDRESS HighAddr;
|
---|
560 | HighAddr.QuadPart = PhysHighest == NIL_RTHCPHYS ? MAXLONGLONG : PhysHighest;
|
---|
561 | PMDL pMdl = g_pfnrtMmAllocatePagesForMdl(Zero, HighAddr, Zero, cb);
|
---|
562 | if (pMdl)
|
---|
563 | {
|
---|
564 | if (MmGetMdlByteCount(pMdl) >= cb)
|
---|
565 | {
|
---|
566 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_PHYS_NC, NULL, cb);
|
---|
567 | if (pMemNt)
|
---|
568 | {
|
---|
569 | pMemNt->fAllocatedPagesForMdl = true;
|
---|
570 | pMemNt->cMdls = 1;
|
---|
571 | pMemNt->apMdls[0] = pMdl;
|
---|
572 | *ppMem = &pMemNt->Core;
|
---|
573 | return VINF_SUCCESS;
|
---|
574 | }
|
---|
575 | }
|
---|
576 | g_pfnrtMmFreePagesFromMdl(pMdl);
|
---|
577 | ExFreePool(pMdl);
|
---|
578 | }
|
---|
579 | return VERR_NO_MEMORY;
|
---|
580 | }
|
---|
581 | return VERR_NOT_SUPPORTED;
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
|
---|
586 | {
|
---|
587 | AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE || uCachePolicy == RTMEM_CACHE_POLICY_MMIO, VERR_NOT_SUPPORTED);
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * Validate the address range and create a descriptor for it.
|
---|
591 | */
|
---|
592 | PFN_NUMBER Pfn = (PFN_NUMBER)(Phys >> PAGE_SHIFT);
|
---|
593 | if (((RTHCPHYS)Pfn << PAGE_SHIFT) != Phys)
|
---|
594 | return VERR_ADDRESS_TOO_BIG;
|
---|
595 |
|
---|
596 | /*
|
---|
597 | * Create the IPRT memory object.
|
---|
598 | */
|
---|
599 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
600 | if (pMemNt)
|
---|
601 | {
|
---|
602 | pMemNt->Core.u.Phys.PhysBase = Phys;
|
---|
603 | pMemNt->Core.u.Phys.fAllocated = false;
|
---|
604 | pMemNt->Core.u.Phys.uCachePolicy = uCachePolicy;
|
---|
605 | *ppMem = &pMemNt->Core;
|
---|
606 | return VINF_SUCCESS;
|
---|
607 | }
|
---|
608 | return VERR_NO_MEMORY;
|
---|
609 | }
|
---|
610 |
|
---|
611 |
|
---|
612 | /**
|
---|
613 | * Internal worker for locking down pages.
|
---|
614 | *
|
---|
615 | * @return IPRT status code.
|
---|
616 | *
|
---|
617 | * @param ppMem Where to store the memory object pointer.
|
---|
618 | * @param pv First page.
|
---|
619 | * @param cb Number of bytes.
|
---|
620 | * @param fAccess The desired access, a combination of RTMEM_PROT_READ
|
---|
621 | * and RTMEM_PROT_WRITE.
|
---|
622 | * @param R0Process The process \a pv and \a cb refers to.
|
---|
623 | */
|
---|
624 | static int rtR0MemObjNtLock(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
|
---|
625 | {
|
---|
626 | /*
|
---|
627 | * Calc the number of MDLs we need and allocate the memory object structure.
|
---|
628 | */
|
---|
629 | size_t cMdls = cb / MAX_LOCK_MEM_SIZE;
|
---|
630 | if (cb % MAX_LOCK_MEM_SIZE)
|
---|
631 | cMdls++;
|
---|
632 | if (cMdls >= UINT32_MAX)
|
---|
633 | return VERR_OUT_OF_RANGE;
|
---|
634 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(RT_UOFFSETOF_DYN(RTR0MEMOBJNT, apMdls[cMdls]),
|
---|
635 | RTR0MEMOBJTYPE_LOCK, pv, cb);
|
---|
636 | if (!pMemNt)
|
---|
637 | return VERR_NO_MEMORY;
|
---|
638 |
|
---|
639 | /*
|
---|
640 | * Loop locking down the sub parts of the memory.
|
---|
641 | */
|
---|
642 | int rc = VINF_SUCCESS;
|
---|
643 | size_t cbTotal = 0;
|
---|
644 | uint8_t *pb = (uint8_t *)pv;
|
---|
645 | uint32_t iMdl;
|
---|
646 | for (iMdl = 0; iMdl < cMdls; iMdl++)
|
---|
647 | {
|
---|
648 | /*
|
---|
649 | * Calc the Mdl size and allocate it.
|
---|
650 | */
|
---|
651 | size_t cbCur = cb - cbTotal;
|
---|
652 | if (cbCur > MAX_LOCK_MEM_SIZE)
|
---|
653 | cbCur = MAX_LOCK_MEM_SIZE;
|
---|
654 | AssertMsg(cbCur, ("cbCur: 0!\n"));
|
---|
655 | PMDL pMdl = IoAllocateMdl(pb, (ULONG)cbCur, FALSE, FALSE, NULL);
|
---|
656 | if (!pMdl)
|
---|
657 | {
|
---|
658 | rc = VERR_NO_MEMORY;
|
---|
659 | break;
|
---|
660 | }
|
---|
661 |
|
---|
662 | /*
|
---|
663 | * Lock the pages.
|
---|
664 | */
|
---|
665 | __try
|
---|
666 | {
|
---|
667 | MmProbeAndLockPages(pMdl,
|
---|
668 | R0Process == NIL_RTR0PROCESS ? KernelMode : UserMode,
|
---|
669 | fAccess == RTMEM_PROT_READ
|
---|
670 | ? IoReadAccess
|
---|
671 | : fAccess == RTMEM_PROT_WRITE
|
---|
672 | ? IoWriteAccess
|
---|
673 | : IoModifyAccess);
|
---|
674 |
|
---|
675 | pMemNt->apMdls[iMdl] = pMdl;
|
---|
676 | pMemNt->cMdls++;
|
---|
677 | }
|
---|
678 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
679 | {
|
---|
680 | IoFreeMdl(pMdl);
|
---|
681 | rc = VERR_LOCK_FAILED;
|
---|
682 | break;
|
---|
683 | }
|
---|
684 |
|
---|
685 | if ( R0Process != NIL_RTR0PROCESS
|
---|
686 | && g_pfnrtMmSecureVirtualMemory
|
---|
687 | && g_pfnrtMmUnsecureVirtualMemory)
|
---|
688 | {
|
---|
689 | /* Make sure the user process can't change the allocation. */
|
---|
690 | pMemNt->pvSecureMem = g_pfnrtMmSecureVirtualMemory(pv, cb,
|
---|
691 | fAccess & RTMEM_PROT_WRITE
|
---|
692 | ? PAGE_READWRITE
|
---|
693 | : PAGE_READONLY);
|
---|
694 | if (!pMemNt->pvSecureMem)
|
---|
695 | {
|
---|
696 | rc = VERR_NO_MEMORY;
|
---|
697 | break;
|
---|
698 | }
|
---|
699 | }
|
---|
700 |
|
---|
701 | /* next */
|
---|
702 | cbTotal += cbCur;
|
---|
703 | pb += cbCur;
|
---|
704 | }
|
---|
705 | if (RT_SUCCESS(rc))
|
---|
706 | {
|
---|
707 | Assert(pMemNt->cMdls == cMdls);
|
---|
708 | pMemNt->Core.u.Lock.R0Process = R0Process;
|
---|
709 | *ppMem = &pMemNt->Core;
|
---|
710 | return rc;
|
---|
711 | }
|
---|
712 |
|
---|
713 | /*
|
---|
714 | * We failed, perform cleanups.
|
---|
715 | */
|
---|
716 | while (iMdl-- > 0)
|
---|
717 | {
|
---|
718 | MmUnlockPages(pMemNt->apMdls[iMdl]);
|
---|
719 | IoFreeMdl(pMemNt->apMdls[iMdl]);
|
---|
720 | pMemNt->apMdls[iMdl] = NULL;
|
---|
721 | }
|
---|
722 | if (pMemNt->pvSecureMem)
|
---|
723 | {
|
---|
724 | if (g_pfnrtMmUnsecureVirtualMemory)
|
---|
725 | g_pfnrtMmUnsecureVirtualMemory(pMemNt->pvSecureMem);
|
---|
726 | pMemNt->pvSecureMem = NULL;
|
---|
727 | }
|
---|
728 |
|
---|
729 | rtR0MemObjDelete(&pMemNt->Core);
|
---|
730 | return rc;
|
---|
731 | }
|
---|
732 |
|
---|
733 |
|
---|
734 | DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess,
|
---|
735 | RTR0PROCESS R0Process)
|
---|
736 | {
|
---|
737 | AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
|
---|
738 | /* (Can use MmProbeAndLockProcessPages if we need to mess with other processes later.) */
|
---|
739 | return rtR0MemObjNtLock(ppMem, (void *)R3Ptr, cb, fAccess, R0Process);
|
---|
740 | }
|
---|
741 |
|
---|
742 |
|
---|
743 | DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
|
---|
744 | {
|
---|
745 | return rtR0MemObjNtLock(ppMem, pv, cb, fAccess, NIL_RTR0PROCESS);
|
---|
746 | }
|
---|
747 |
|
---|
748 |
|
---|
749 | DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
|
---|
750 | {
|
---|
751 | /*
|
---|
752 | * MmCreateSection(SEC_RESERVE) + MmMapViewInSystemSpace perhaps?
|
---|
753 | */
|
---|
754 | RT_NOREF4(ppMem, pvFixed, cb, uAlignment);
|
---|
755 | return VERR_NOT_SUPPORTED;
|
---|
756 | }
|
---|
757 |
|
---|
758 |
|
---|
759 | DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment,
|
---|
760 | RTR0PROCESS R0Process)
|
---|
761 | {
|
---|
762 | /*
|
---|
763 | * ZeCreateSection(SEC_RESERVE) + ZwMapViewOfSection perhaps?
|
---|
764 | */
|
---|
765 | RT_NOREF5(ppMem, R3PtrFixed, cb, uAlignment, R0Process);
|
---|
766 | return VERR_NOT_SUPPORTED;
|
---|
767 | }
|
---|
768 |
|
---|
769 |
|
---|
770 | /**
|
---|
771 | * Internal worker for rtR0MemObjNativeMapKernel and rtR0MemObjNativeMapUser.
|
---|
772 | *
|
---|
773 | * @returns IPRT status code.
|
---|
774 | * @param ppMem Where to store the memory object for the mapping.
|
---|
775 | * @param pMemToMap The memory object to map.
|
---|
776 | * @param pvFixed Where to map it. (void *)-1 if anywhere is fine.
|
---|
777 | * @param uAlignment The alignment requirement for the mapping.
|
---|
778 | * @param fProt The desired page protection for the mapping.
|
---|
779 | * @param R0Process If NIL_RTR0PROCESS map into system (kernel) memory.
|
---|
780 | * If not nil, it's the current process.
|
---|
781 | * @param offSub Offset into @a pMemToMap to start mapping.
|
---|
782 | * @param cbSub The number of bytes to map from @a pMapToMem. 0 if
|
---|
783 | * we're to map everything. Non-zero if @a offSub is
|
---|
784 | * non-zero.
|
---|
785 | */
|
---|
786 | static int rtR0MemObjNtMap(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
|
---|
787 | unsigned fProt, RTR0PROCESS R0Process, size_t offSub, size_t cbSub)
|
---|
788 | {
|
---|
789 | int rc = VERR_MAP_FAILED;
|
---|
790 |
|
---|
791 | /*
|
---|
792 | * Check that the specified alignment is supported.
|
---|
793 | */
|
---|
794 | if (uAlignment > PAGE_SIZE)
|
---|
795 | return VERR_NOT_SUPPORTED;
|
---|
796 |
|
---|
797 | /*
|
---|
798 | * There are two basic cases here, either we've got an MDL and can
|
---|
799 | * map it using MmMapLockedPages, or we've got a contiguous physical
|
---|
800 | * range (MMIO most likely) and can use MmMapIoSpace.
|
---|
801 | */
|
---|
802 | PRTR0MEMOBJNT pMemNtToMap = (PRTR0MEMOBJNT)pMemToMap;
|
---|
803 | if (pMemNtToMap->cMdls)
|
---|
804 | {
|
---|
805 | /* don't attempt map locked regions with more than one mdl. */
|
---|
806 | if (pMemNtToMap->cMdls != 1)
|
---|
807 | return VERR_NOT_SUPPORTED;
|
---|
808 |
|
---|
809 | /* Need g_pfnrtMmMapLockedPagesSpecifyCache to map to a specific address. */
|
---|
810 | if (pvFixed != (void *)-1 && g_pfnrtMmMapLockedPagesSpecifyCache == NULL)
|
---|
811 | return VERR_NOT_SUPPORTED;
|
---|
812 |
|
---|
813 | /* we can't map anything to the first page, sorry. */
|
---|
814 | if (pvFixed == 0)
|
---|
815 | return VERR_NOT_SUPPORTED;
|
---|
816 |
|
---|
817 | /* only one system mapping for now - no time to figure out MDL restrictions right now. */
|
---|
818 | if ( pMemNtToMap->Core.uRel.Parent.cMappings
|
---|
819 | && R0Process == NIL_RTR0PROCESS)
|
---|
820 | {
|
---|
821 | if (pMemNtToMap->Core.enmType != RTR0MEMOBJTYPE_PHYS_NC)
|
---|
822 | return VERR_NOT_SUPPORTED;
|
---|
823 | uint32_t iMapping = pMemNtToMap->Core.uRel.Parent.cMappings;
|
---|
824 | while (iMapping-- > 0)
|
---|
825 | {
|
---|
826 | PRTR0MEMOBJNT pMapping = (PRTR0MEMOBJNT)pMemNtToMap->Core.uRel.Parent.papMappings[iMapping];
|
---|
827 | if ( pMapping->Core.enmType != RTR0MEMOBJTYPE_MAPPING
|
---|
828 | || pMapping->Core.u.Mapping.R0Process == NIL_RTR0PROCESS)
|
---|
829 | return VERR_NOT_SUPPORTED;
|
---|
830 | }
|
---|
831 | }
|
---|
832 |
|
---|
833 | /* Create a partial MDL if this is a sub-range request. */
|
---|
834 | PMDL pMdl;
|
---|
835 | if (!offSub && !cbSub)
|
---|
836 | pMdl = pMemNtToMap->apMdls[0];
|
---|
837 | else
|
---|
838 | {
|
---|
839 | pMdl = IoAllocateMdl(NULL, (ULONG)cbSub, FALSE, FALSE, NULL);
|
---|
840 | if (pMdl)
|
---|
841 | IoBuildPartialMdl(pMemNtToMap->apMdls[0], pMdl,
|
---|
842 | (uint8_t *)MmGetMdlVirtualAddress(pMemNtToMap->apMdls[0]) + offSub, (ULONG)cbSub);
|
---|
843 | else
|
---|
844 | {
|
---|
845 | IoFreeMdl(pMdl);
|
---|
846 | return VERR_NO_MEMORY;
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | __try
|
---|
851 | {
|
---|
852 | /** @todo uAlignment */
|
---|
853 | /** @todo How to set the protection on the pages? */
|
---|
854 | void *pv;
|
---|
855 | if (g_pfnrtMmMapLockedPagesSpecifyCache)
|
---|
856 | pv = g_pfnrtMmMapLockedPagesSpecifyCache(pMdl,
|
---|
857 | R0Process == NIL_RTR0PROCESS ? KernelMode : UserMode,
|
---|
858 | MmCached,
|
---|
859 | pvFixed != (void *)-1 ? pvFixed : NULL,
|
---|
860 | FALSE /* no bug check on failure */,
|
---|
861 | NormalPagePriority);
|
---|
862 | else
|
---|
863 | pv = MmMapLockedPages(pMdl, R0Process == NIL_RTR0PROCESS ? KernelMode : UserMode);
|
---|
864 | if (pv)
|
---|
865 | {
|
---|
866 | NOREF(fProt);
|
---|
867 |
|
---|
868 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew( !offSub && !cbSub
|
---|
869 | ? sizeof(*pMemNt) : RT_UOFFSETOF_DYN(RTR0MEMOBJNT, apMdls[1]),
|
---|
870 | RTR0MEMOBJTYPE_MAPPING, pv, pMemNtToMap->Core.cb);
|
---|
871 | if (pMemNt)
|
---|
872 | {
|
---|
873 | pMemNt->Core.u.Mapping.R0Process = R0Process;
|
---|
874 | if (!offSub && !cbSub)
|
---|
875 | pMemNt->fSubMapping = false;
|
---|
876 | else
|
---|
877 | {
|
---|
878 | pMemNt->apMdls[0] = pMdl;
|
---|
879 | pMemNt->cMdls = 1;
|
---|
880 | pMemNt->fSubMapping = true;
|
---|
881 | }
|
---|
882 |
|
---|
883 | *ppMem = &pMemNt->Core;
|
---|
884 | return VINF_SUCCESS;
|
---|
885 | }
|
---|
886 |
|
---|
887 | rc = VERR_NO_MEMORY;
|
---|
888 | MmUnmapLockedPages(pv, pMdl);
|
---|
889 | }
|
---|
890 | }
|
---|
891 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
892 | {
|
---|
893 | #ifdef LOG_ENABLED
|
---|
894 | NTSTATUS rcNt = GetExceptionCode();
|
---|
895 | Log(("rtR0MemObjNtMap: Exception Code %#x\n", rcNt));
|
---|
896 | #endif
|
---|
897 |
|
---|
898 | /* nothing */
|
---|
899 | rc = VERR_MAP_FAILED;
|
---|
900 | }
|
---|
901 |
|
---|
902 | }
|
---|
903 | else
|
---|
904 | {
|
---|
905 | AssertReturn( pMemNtToMap->Core.enmType == RTR0MEMOBJTYPE_PHYS
|
---|
906 | && !pMemNtToMap->Core.u.Phys.fAllocated, VERR_INTERNAL_ERROR);
|
---|
907 |
|
---|
908 | /* cannot map phys mem to user space (yet). */
|
---|
909 | if (R0Process != NIL_RTR0PROCESS)
|
---|
910 | return VERR_NOT_SUPPORTED;
|
---|
911 |
|
---|
912 | /* Cannot sub-mak these (yet). */
|
---|
913 | AssertMsgReturn(!offSub && !cbSub, ("%#zx %#zx\n", offSub, cbSub), VERR_NOT_SUPPORTED);
|
---|
914 |
|
---|
915 |
|
---|
916 | /** @todo uAlignment */
|
---|
917 | /** @todo How to set the protection on the pages? */
|
---|
918 | PHYSICAL_ADDRESS Phys;
|
---|
919 | Phys.QuadPart = pMemNtToMap->Core.u.Phys.PhysBase;
|
---|
920 | void *pv = MmMapIoSpace(Phys, pMemNtToMap->Core.cb,
|
---|
921 | pMemNtToMap->Core.u.Phys.uCachePolicy == RTMEM_CACHE_POLICY_MMIO ? MmNonCached : MmCached);
|
---|
922 | if (pv)
|
---|
923 | {
|
---|
924 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)rtR0MemObjNew(sizeof(*pMemNt), RTR0MEMOBJTYPE_MAPPING, pv,
|
---|
925 | pMemNtToMap->Core.cb);
|
---|
926 | if (pMemNt)
|
---|
927 | {
|
---|
928 | pMemNt->Core.u.Mapping.R0Process = R0Process;
|
---|
929 | *ppMem = &pMemNt->Core;
|
---|
930 | return VINF_SUCCESS;
|
---|
931 | }
|
---|
932 |
|
---|
933 | rc = VERR_NO_MEMORY;
|
---|
934 | MmUnmapIoSpace(pv, pMemNtToMap->Core.cb);
|
---|
935 | }
|
---|
936 | }
|
---|
937 |
|
---|
938 | NOREF(uAlignment); NOREF(fProt);
|
---|
939 | return rc;
|
---|
940 | }
|
---|
941 |
|
---|
942 |
|
---|
943 | DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
|
---|
944 | unsigned fProt, size_t offSub, size_t cbSub)
|
---|
945 | {
|
---|
946 | return rtR0MemObjNtMap(ppMem, pMemToMap, pvFixed, uAlignment, fProt, NIL_RTR0PROCESS, offSub, cbSub);
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment,
|
---|
951 | unsigned fProt, RTR0PROCESS R0Process, size_t offSub, size_t cbSub)
|
---|
952 | {
|
---|
953 | AssertReturn(R0Process == RTR0ProcHandleSelf(), VERR_NOT_SUPPORTED);
|
---|
954 | return rtR0MemObjNtMap(ppMem, pMemToMap, (void *)R3PtrFixed, uAlignment, fProt, R0Process, offSub, cbSub);
|
---|
955 | }
|
---|
956 |
|
---|
957 |
|
---|
958 | DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
|
---|
959 | {
|
---|
960 | #if 0
|
---|
961 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)pMem;
|
---|
962 | #endif
|
---|
963 |
|
---|
964 | /*
|
---|
965 | * Seems there are some issues with this MmProtectMdlSystemAddress API, so
|
---|
966 | * this code isn't currently enabled until we've tested it with the verifier.
|
---|
967 | */
|
---|
968 | #if 0
|
---|
969 | /*
|
---|
970 | * The API we've got requires a kernel mapping.
|
---|
971 | */
|
---|
972 | if ( pMemNt->cMdls
|
---|
973 | && g_pfnrtMmProtectMdlSystemAddress
|
---|
974 | && (g_uRtNtMajorVer > 6 || (g_uRtNtMajorVer == 6 && g_uRtNtMinorVer >= 1)) /* Windows 7 and later. */
|
---|
975 | && pMemNt->Core.pv != NULL
|
---|
976 | && ( pMemNt->Core.enmType == RTR0MEMOBJTYPE_PAGE
|
---|
977 | || pMemNt->Core.enmType == RTR0MEMOBJTYPE_LOW
|
---|
978 | || pMemNt->Core.enmType == RTR0MEMOBJTYPE_CONT
|
---|
979 | || ( pMemNt->Core.enmType == RTR0MEMOBJTYPE_LOCK
|
---|
980 | && pMemNt->Core.u.Lock.R0Process == NIL_RTPROCESS)
|
---|
981 | || ( pMemNt->Core.enmType == RTR0MEMOBJTYPE_MAPPING
|
---|
982 | && pMemNt->Core.u.Mapping.R0Process == NIL_RTPROCESS) ) )
|
---|
983 | {
|
---|
984 | /* Convert the protection. */
|
---|
985 | LOCK_OPERATION enmLockOp;
|
---|
986 | ULONG fAccess;
|
---|
987 | switch (fProt)
|
---|
988 | {
|
---|
989 | case RTMEM_PROT_NONE:
|
---|
990 | fAccess = PAGE_NOACCESS;
|
---|
991 | enmLockOp = IoReadAccess;
|
---|
992 | break;
|
---|
993 | case RTMEM_PROT_READ:
|
---|
994 | fAccess = PAGE_READONLY;
|
---|
995 | enmLockOp = IoReadAccess;
|
---|
996 | break;
|
---|
997 | case RTMEM_PROT_WRITE:
|
---|
998 | case RTMEM_PROT_WRITE | RTMEM_PROT_READ:
|
---|
999 | fAccess = PAGE_READWRITE;
|
---|
1000 | enmLockOp = IoModifyAccess;
|
---|
1001 | break;
|
---|
1002 | case RTMEM_PROT_EXEC:
|
---|
1003 | fAccess = PAGE_EXECUTE;
|
---|
1004 | enmLockOp = IoReadAccess;
|
---|
1005 | break;
|
---|
1006 | case RTMEM_PROT_EXEC | RTMEM_PROT_READ:
|
---|
1007 | fAccess = PAGE_EXECUTE_READ;
|
---|
1008 | enmLockOp = IoReadAccess;
|
---|
1009 | break;
|
---|
1010 | case RTMEM_PROT_EXEC | RTMEM_PROT_WRITE:
|
---|
1011 | case RTMEM_PROT_EXEC | RTMEM_PROT_WRITE | RTMEM_PROT_READ:
|
---|
1012 | fAccess = PAGE_EXECUTE_READWRITE;
|
---|
1013 | enmLockOp = IoModifyAccess;
|
---|
1014 | break;
|
---|
1015 | default:
|
---|
1016 | AssertFailedReturn(VERR_INVALID_FLAGS);
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | NTSTATUS rcNt = STATUS_SUCCESS;
|
---|
1020 | # if 0 /** @todo test this against the verifier. */
|
---|
1021 | if (offSub == 0 && pMemNt->Core.cb == cbSub)
|
---|
1022 | {
|
---|
1023 | uint32_t iMdl = pMemNt->cMdls;
|
---|
1024 | while (iMdl-- > 0)
|
---|
1025 | {
|
---|
1026 | rcNt = g_pfnrtMmProtectMdlSystemAddress(pMemNt->apMdls[i], fAccess);
|
---|
1027 | if (!NT_SUCCESS(rcNt))
|
---|
1028 | break;
|
---|
1029 | }
|
---|
1030 | }
|
---|
1031 | else
|
---|
1032 | # endif
|
---|
1033 | {
|
---|
1034 | /*
|
---|
1035 | * We ASSUME the following here:
|
---|
1036 | * - MmProtectMdlSystemAddress can deal with nonpaged pool memory
|
---|
1037 | * - MmProtectMdlSystemAddress doesn't actually store anything in the MDL we pass it.
|
---|
1038 | * - We are not required to call MmProtectMdlSystemAddress with PAGE_READWRITE for the
|
---|
1039 | * exact same ranges prior to freeing them.
|
---|
1040 | *
|
---|
1041 | * So, we lock the pages temporarily, call the API and unlock them.
|
---|
1042 | */
|
---|
1043 | uint8_t *pbCur = (uint8_t *)pMemNt->Core.pv + offSub;
|
---|
1044 | while (cbSub > 0 && NT_SUCCESS(rcNt))
|
---|
1045 | {
|
---|
1046 | size_t cbCur = cbSub;
|
---|
1047 | if (cbCur > MAX_LOCK_MEM_SIZE)
|
---|
1048 | cbCur = MAX_LOCK_MEM_SIZE;
|
---|
1049 | PMDL pMdl = IoAllocateMdl(pbCur, (ULONG)cbCur, FALSE, FALSE, NULL);
|
---|
1050 | if (pMdl)
|
---|
1051 | {
|
---|
1052 | __try
|
---|
1053 | {
|
---|
1054 | MmProbeAndLockPages(pMdl, KernelMode, enmLockOp);
|
---|
1055 | }
|
---|
1056 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
1057 | {
|
---|
1058 | rcNt = GetExceptionCode();
|
---|
1059 | }
|
---|
1060 | if (NT_SUCCESS(rcNt))
|
---|
1061 | {
|
---|
1062 | rcNt = g_pfnrtMmProtectMdlSystemAddress(pMdl, fAccess);
|
---|
1063 | MmUnlockPages(pMdl);
|
---|
1064 | }
|
---|
1065 | IoFreeMdl(pMdl);
|
---|
1066 | }
|
---|
1067 | else
|
---|
1068 | rcNt = STATUS_NO_MEMORY;
|
---|
1069 | pbCur += cbCur;
|
---|
1070 | cbSub -= cbCur;
|
---|
1071 | }
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | if (NT_SUCCESS(rcNt))
|
---|
1075 | return VINF_SUCCESS;
|
---|
1076 | return RTErrConvertFromNtStatus(rcNt);
|
---|
1077 | }
|
---|
1078 | #else
|
---|
1079 | RT_NOREF4(pMem, offSub, cbSub, fProt);
|
---|
1080 | #endif
|
---|
1081 |
|
---|
1082 | return VERR_NOT_SUPPORTED;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 |
|
---|
1086 | DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
|
---|
1087 | {
|
---|
1088 | PRTR0MEMOBJNT pMemNt = (PRTR0MEMOBJNT)pMem;
|
---|
1089 |
|
---|
1090 | if (pMemNt->cMdls)
|
---|
1091 | {
|
---|
1092 | if (pMemNt->cMdls == 1)
|
---|
1093 | {
|
---|
1094 | PPFN_NUMBER paPfns = MmGetMdlPfnArray(pMemNt->apMdls[0]);
|
---|
1095 | return (RTHCPHYS)paPfns[iPage] << PAGE_SHIFT;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | size_t iMdl = iPage / (MAX_LOCK_MEM_SIZE >> PAGE_SHIFT);
|
---|
1099 | size_t iMdlPfn = iPage % (MAX_LOCK_MEM_SIZE >> PAGE_SHIFT);
|
---|
1100 | PPFN_NUMBER paPfns = MmGetMdlPfnArray(pMemNt->apMdls[iMdl]);
|
---|
1101 | return (RTHCPHYS)paPfns[iMdlPfn] << PAGE_SHIFT;
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | switch (pMemNt->Core.enmType)
|
---|
1105 | {
|
---|
1106 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
1107 | return rtR0MemObjNativeGetPagePhysAddr(pMemNt->Core.uRel.Child.pParent, iPage);
|
---|
1108 |
|
---|
1109 | case RTR0MEMOBJTYPE_PHYS:
|
---|
1110 | return pMemNt->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
|
---|
1111 |
|
---|
1112 | case RTR0MEMOBJTYPE_PAGE:
|
---|
1113 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
1114 | case RTR0MEMOBJTYPE_LOW:
|
---|
1115 | case RTR0MEMOBJTYPE_CONT:
|
---|
1116 | case RTR0MEMOBJTYPE_LOCK:
|
---|
1117 | default:
|
---|
1118 | AssertMsgFailed(("%d\n", pMemNt->Core.enmType));
|
---|
1119 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
1120 | return NIL_RTHCPHYS;
|
---|
1121 | }
|
---|
1122 | }
|
---|
1123 |
|
---|