Creating and Using Mdl

  1. http://msdn.microsoft.com/en-us/windows/hardware/gg463193

states that IoAllocateMdl and MmBuildMdlForNonpagedPool can be used for
buffers allocated using ExAllocatePoolWithTag. Is it possible to use
buffers obtained for a non paged lookaside list.

  1. It also states the following.“When the driver completes its
    manipulations, it should copy the changed data from the temporary MDL back
    to the system-supplied MDL by using *RtlCopyMemory* within a *try/except*
    or *try/finally* block.”

If we want to encrypt data on a read and allocate a Buffer in pre op for
MDL_READ, will I have to perform the copy on post op or can I simply free
the Mdl in MDL_READ_COMPLETE(pre-op or post-op)?

Thanks,
Arushi

Reposting questions, as I havent heard any responses.

  1. http://msdn.microsoft.com/en-us/windows/hardware/gg463193

states that IoAllocateMdl and MmBuildMdlForNonpagedPool can be used for
buffers allocated using ExAllocatePoolWithTag. Is it possible to use
buffers obtained from a non paged lookaside list.

  1. It also states the following.“When the driver completes its
    manipulations, it should copy the changed data from the temporary MDL back
    to the system-supplied MDL by using *RtlCopyMemory* within a *try/except*
    or *try/finally* block.”
    If we want to encrypt data on a read and allocate a Buffer in pre op for
    MDL_READ, will I have to perform the copy on post op or can I simply free
    the Mdl in MDL_READ_COMPLETE(pre-op or post-op)?

Also, is there a way to know which driver allocated an MDL given its
address using windbg?

Thanks,
Arushi

On Sat, Dec 31, 2011 at 9:59 PM, Arushi Aggarwal wrote:

> 1. http://msdn.microsoft.com/en-us/windows/hardware/gg463193
>
> states that IoAllocateMdl and MmBuildMdlForNonpagedPool can be used for
> buffers allocated using ExAllocatePoolWithTag. Is it possible to use
> buffers obtained for a non paged lookaside list.
>
> 2. It also states the following.“When the driver completes its
> manipulations, it should copy the changed data from the temporary MDL back
> to the system-supplied MDL by using RtlCopyMemory within a try/except
> or try/finally block.”
>
> If we want to encrypt data on a read and allocate a Buffer in pre op for
> MDL_READ, will I have to perform the copy on post op or can I simply free
> the Mdl in MDL_READ_COMPLETE(pre-op or post-op)?
>
> Thanks,
> Arushi
>

It should be fine.

I don’t understand the question.

It depends on where you put the data, right? The data goes wherever you point the MDL. So, if after the read is completed you want the data to appear in the buffer that the system/user specified, and your MDL puts the data someplace else, your MDL will either need to point there or you’ll need to copy the data to that location.

No.

Peter
OSR

>

I don’t understand the question.

It depends on where you put the data, right? The data goes wherever you
point the MDL. So, if after the read is completed you want the data to
appear in the buffer that the system/user specified, and your MDL puts the
data someplace else, your MDL will either need to point there or you’ll
need to copy the data to that location.

In PreMdlRead, I allocate a buffer from NonPaged memory to hold the data,
and create an Mdl to point to that. I need to free this buffer after the
read completes. How can I get the address of the buffer in
PreMdlReadComplete? Should I use MmGetMdlVirtualAddress
or MmGetSystemAddressForMdlSafe?

Thanks
Arushi

No.

Peter
OSR


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

You should use MmGetSystemAddressForMdlSafe

Peter
OSR

Shouldn’t both MmGetSystemAddressForMdlSafe and MmGetMdlVirtualAddress,
return the same value for an Mdl built using a nonpaged pool
buffer(MmBuildMdlForNonPagedPool).

I assumed from reading
http://www.osronline.com/showThread.cfm?link=29488
that they would give the same value for the buffer.

I am trying to understand the difference.

Thanks.

On Thu, Jan 5, 2012 at 11:11 AM, wrote:

> You should use MmGetSystemAddressForMdlSafe
>
> Peter
> OSR
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>

>

Shouldn’t both MmGetSystemAddressForMdlSafe and
MmGetMdlVirtualAddress, return the same value for an Mdl built using a
nonpaged pool buffer(MmBuildMdlForNonPagedPool).

I assumed from reading
http://www.osronline.com/showThread.cfm?link=29488
that they would give the same value for the buffer.

I am trying to understand the difference.

In that case they should give the same value as it is already mapped to a System Address. I can’t see a reason why you couldn’t use MmGetMdlVirtualAddress if you know that you created the Mdl from a buffer that you allocated yourself, but maybe there is a reason?

James

Well… they may or may not return exactly the same address.

Also, there is the issue of how your structure your code and show its intent.

MmGetMdlVirtualAddress is designed to get you the starting VA of the buffer as mapped into the original requesting process context. There’s actually very little use for this field, except for some of the (pre-Windows 8) WDM DMA APIs… where it’s use is a bit strange in any case.

MmGetSystemAddressForMdlSafe is designed to return the address of the data buffer described by the MDL mapped into the “system” part of kernel virtual address space. When you want to refer to a buffer described by an MDL in your kernel mode driver using WDM functions, this is the function to call.

When I’m called in to fix some bizzare bug in your driver 5 years from now, after you’ve become rich and famous or something, and I see you calling MmGetMdlVirtualAddress in your driver and then subsequently dereferencing the returned pointer – unless you’ve provided copious comments about why you’re doing this – that’s going to set alarm bells off with me, and make me wonder if you were using the function correctly. And if you DID take the time to provide the copious comments, why not just call the “right” most commonly expected function in the first place, and save future maintainers the guesswork?

FINALLY, there’s the question about whether MmGetSystemAddressForMdlSafe() == MmGetMdlVirtualAddress() – MmGetMdlVirtualAddress will return the base address of the first PAGE of the MDL and does not take into account Mdl->ByteOffset (the offset from the start of the first page to the first byte of the described data buffer). MmGetSystemAddressForMdlSafe, on the other hand, returns you the KVA of the first byte of the user data buffer.

Of course, for a non-paged pool buffer, it’s most likely the buffer you allocate from non-paged pool will be 4K or more, and will therefore be page aligned. So… they both will PROBABLY return the same address. Except when they don’t.

I hope that helps,

Peter
OSR

wrote in message news:xxxxx@ntdev…

> FINALLY, there’s the question about whether MmGetSystemAddressForMdlSafe()
> == MmGetMdlVirtualAddress()
– MmGetMdlVirtualAddress will return the base address of the first PAGE of
the MDL and does
not take into account Mdl->ByteOffset (the offset from the start of the
first page to the first byte of the described data buffer).

??? Sorry but could you just look at the definition, in wdm.h?

#define MmGetMdlVirtualAddress(Mdl) <br> ((PVOID) ((PCHAR) ((Mdl)->StartVa) + (Mdl)->ByteOffset))

Regards,
– pa

Sorry, you’re right…

My bad for assuming MmGetMdLVirtualAddress just fetched Mdl->StartingVa. I should have checked.

Then again, that’s another reason not to use MmGetMdlVirtualAddress… nobody uses it except for DMA. And while, yes, I should have looked in WDM.H to answer the question, I bet I’m not the only person who would read code that used this function for non-DMA purposes and made a bad assumption.

No excusing the lack of thoroughness, but just sayin’…

Peter
OSR