How to set the offset in MmMapLockedPagesWithReservedMapping?

Hello,

I see no way to map the sub-range of VA with MmMapLockedPagesWithReservedMapping() function even though the MSDN document says one can do that when calling IoAllocateMdl() function. This is the problem I’m trying to solve:

a) The driver receives MDLs from Windows kernel that gives physical page information for each page along with the offset of the page in the allocation. Lets say the allocation is 40KB in size, the driver will receive MDL information for each page which will be called 10 times.

b) It becomes a herculean task for the driver to look-up these MDLs (think allocation size is 32MB) when it tries to read/write into the pages of the allocation.

c) To solve this, I reserved VA with MmAllocateMappingAddress() function for 32MB and thought of using MmMapLockedPagesWithReservedMapping() function to map each physical page information I receive from the kernel into the reserved VA space. This will make reading/writing to the pages much easier later on without any look-ups.

First MDL for page 1 comes in, driver calls MmMapLockedPagesWithReservedMapping() and everythings looks ok. Second MDL for page 2 comes in but I don’t know how to insert offset into the reserved VA space. Is it possible to do that? MSDN says we can map sub-ranges in the VA spaces but unsure of how exactly to do it?

Can anyone shed some light on this? Thanks for your time.

I’m confused by step (b) of your problem description. Drivers do not
write to physical pages they write to virtual pages. You need the
offset and the va from each mdl once.

I also don’t understand what you are actually trying to accomplish,
outside of my confusion over (b).

Mark Roddy

On Tue, Jun 14, 2011 at 5:36 PM, wrote:
> MmMapLockedPagesWithReservedMapping

I apologize for the confusion. I understand that drivers write into virtual addresses and not the physical pages. I will try to rephrase the description with an example below…

Lets say Windows creates a 40KB buffer outside of the driver and gives the description of the buffer to the driver in form of MDLs. One MDL for each physical page backing the buffer. So, now the driver has 10 MDLs that describes the entire buffer.

If the driver wants to read/write into one particular page, it can retrieve the appropriate MDL, map the physical pages to a virtual address using any of MmMap* functions and access the bits there. This is good enough when it wants to access one page but it becomes an overhead when the driver wants to access multiple pages at one go. That wouldn’t be impossible to do but just that it has to go through the above mentioned steps for every page.

In order to optimize this, I was looking at MSDN documentation and came across MmAllocateMappingAddress()/MmMapLockedPagesWithReservedMapping() functions which looks promising where I can reserve VA for a 40KB buffer initially and then map each physical page into this virtual address as the kernel provides me the MDLs for each page. This will allow the driver to use the reserved virtual address to access the pages at one go without bothering about look-up of MDLs and mapping the physical pages.

Is this possible to do with MmMapLockedPagesWithReservedMapping() as I don’t see how I can provide the offset into the reserved VA in the function?

Thanks.

xxxxx@yahoo.com wrote:

Lets say Windows creates a 40KB buffer outside of the driver and gives the description of the buffer to the driver in form of MDLs. One MDL for each physical page backing the buffer. So, now the driver has 10 MDLs that describes the entire buffer.

No. The driver has one MDL containing 10 page entries.

If the driver wants to read/write into one particular page, it can retrieve the appropriate MDL, map the physical pages to a virtual address using any of MmMap* functions and access the bits there. This is good enough when it wants to access one page but it becomes an overhead when the driver wants to access multiple pages at one go. That wouldn’t be impossible to do but just that it has to go through the above mentioned steps for every page.

Why wouldn’t you just map the whole buffer in one shot? And how did you
get a 40KB buffer that doesn’t have a virtual address to begin with?


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

On Tue, Jun 14, 2011 at 8:31 PM, wrote:
> Lets say Windows creates a 40KB buffer outside of the driver and gives the description of the buffer to the driver in form of MDLs. One MDL for each physical page backing the buffer. So, now the driver has 10 MDLs that describes the entire buffer.
>
I had to stop reading there. Perhaps it got better. “One MDL for each
physical page”? Why? How about one MDL describing all of the physical
pages that compose the 40KB buffer?

Mark Roddy