In Linux kernel this problem can be solved really easy. You can just
walk on all relevant pages of specific range of memory and lock them.
In Win I know only one method I did once and it is good for application
with all time life memory.
You should
- Use MAXMEM=XXX switch to reserve memory XXX for your application.
IN user mode
- Pass some pointers from user mode to driver, assume "p_vir"
IN driver
- Use something like that:
PHYSICAL_ADDRESS phys_addr;
phys_addr.HighPart = 0;
phys_addr.LowPart = (ULONG) p_vir;
maps the given physical address range to nonpaged system space
IoMapedBaseVa = MmMapIoSpace(phys_addr ,size, MmCached);
value must be less than PAGE_SIZE * (65535 - sizeof(MDL)) /
sizeof(ULONG_PTR)
NonPagedPoolMdl = IoAllocateMdl(IoMapedBaseVa, size,FALSE,FALSE,NULL);
After IoAllocateMdl driver should call MmBuildMdlForNonPagedPool with
the MDL allocated
to set up an MDL describing a driver-allocated buffer in nonpaged pool.
The MmBuildMdlForNonPagedPool routine receives an MDL that specifies a
virtual memory buffer
in nonpaged pool, and updates it to describe the underlying physical
pages.
MmBuildMdlForNonPagedPool(NonPagedPoolMdl);
Now you should lock MmMapLockedPages but the routine is obsolete for
2000 and later,
UserVirtualAddress =
(PVOID)(((ULONG)PAGE_ALIGN(MmMapLockedPages(NonPagedPoolMdl,
UserMode))) + MmGetMdlByteOffset(NonPagedPoolMdl));
insteed use
UserVirtualAddress = MmMapLockedPagesSpecifyCache(NonPagedPoolMdl,
UserMode,
MmCached,
...
NormalPagePriority);
Here you have virtual address for using in User space, but before you
pass it up, first unmap space you taken.
MmUnmapIoSpace(IoMapedBaseVa,size);
Pass UserVirtualAddress to user .
Now you can complete your routine.
back to user space and there to use your pointer (UserVirtualAddress)
Enjoy
Emil E.H
You wrote :
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of nayan kumar
Sent: Wednesday, April 09, 2008 3:57 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Is it possible to lock the memory in kernel driver
Hi All,
Is it possible to lock the memory in kernel driver allocated by
malloc function in user mode.user wants to use that memory depending on
his need without freeing the memory during his work.
At the time of exiting the application user wants to free that memory.
user also wants the physical address of that memory once it will be
locked in the kernel driver.
Is it possible to achieve this. if yes how and if not why.
Regards
Windows Live Spaces : Help your online world come to life, add 500
photos a month. Try it! http:</http:>
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
To unsubscribe, visit the List Server section of OSR Online at