VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/memobj-r0drv-nt.cpp@ 91449

Last change on this file since 91449 was 91446, checked in by vboxsync, 3 years ago

IPRT/memobj: Adding RTR0MemObjAllocLarge for speeding up large page allocations. bugref:5324

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette