MmMapLockedPagesSpecifyCache doubt

I have a 32 bit app and 64 bit driver.
I use MmMapLockedPagesSpecifyCache function in my driver and it returns an address to the mapped pages.
I typecast that to ULONGLONG and then to ULONG so that my 32 bit applictaion can use it.

I am not sure whetehr this is correct or not.
can it be done like this.

In short: Yes.

If I understand correctly, the root question buried deep in that post is: “In a 64-bit Windows system, when my 64-bit driver is called from a 32-bit application… While I am running in the context of the requesting (32-bit) user process, if my driver mas some memory into user space using MmMapLockedPagesSpecifyCache, will that address be within the low 2GB of user virtual address space… such that I can just truncate it from 64-bits to 32-bit and hand it to the user??”

So, the answer is “yes”.

Though it’ll work, I’m not a fan of the ULONGLONG to ULONG cast. I’d recommend you use PtrToUlong(…) to do the truncation. After appropriately asserting the high part of the VA is zero for debugging purposes (and to allay the fears of future maintainers).

Peter
OSR

Yeah Thanks peter.
That was my doubt.

will that address which is returned by MmMapLockedPagesSpecifyCache will always be less than 2GB.

Is it since the OS is a 32 bit one the address is always less than 2 GB???

It will be a 32 bit number, but not necessarily bound to 2gb

d

Sent from my phone with no t9, all spilling mistakes are not intentional.

-----Original Message-----
From: xxxxx@yahoo.co.in
Sent: Thursday, August 06, 2009 7:48 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] MmMapLockedPagesSpecifyCache doubt

Minor correction: it’ll be within the valid user address space for the process.

A 32-bit app that’s flagged as /LARGEADDRESSAWARE:YES will be able to use the top 2GB, too, so make sure not to reuse the top bit for something “special”.

  • S

-----Original Message-----
From: xxxxx@osr.com
Sent: Thursday, August 06, 2009 07:01
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] MmMapLockedPagesSpecifyCache doubt

In short: Yes.

If I understand correctly, the root question buried deep in that post is: “In a 64-bit Windows system, when my 64-bit driver is called from a 32-bit application… While I am running in the context of the requesting (32-bit) user process, if my driver mas some memory into user space using MmMapLockedPagesSpecifyCache, will that address be within the low 2GB of user virtual address space… such that I can just truncate it from 64-bits to 32-bit and hand it to the user??”

So, the answer is “yes”.

Though it’ll work, I’m not a fan of the ULONGLONG to ULONG cast. I’d recommend you use PtrToUlong(…) to do the truncation. After appropriately asserting the high part of the VA is zero for debugging purposes (and to allay the fears of future maintainers).

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

and

Ah! Absolutely correct. Good pickup.

I always forget about the (@#$^&*) /LARGEADDRESS thing. Yucko!

Thanks for the correction,

Peter
OSR

> I typecast that to ULONGLONG and then to ULONG so that my 32 bit applictaion can use it.

Never ever do this, use ULONG_PTR


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

> Is it since the OS is a 32 bit one the address is always less than 2 GB???

On 32bit OS - yes (sometimes 3GB, sometimes 2GB)

For 32bit process in 64bit OS - too. Any functions which deal with user memory will never allocate/create a VAD which goes beyound the 32bit-accessible 4GB.

More so, skipping the LARGEADDRESSAWARE linker flag when building the EXE will force the 64bit kernel to place all VADs below 3GB, even not 4GB.


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

> I typecast that to ULONGLONG and then to ULONG so that my 32 bit applictaion
can use it.

Never ever do this, use ULONG_PTR

should I type cast the void pointer returned from MmMapLockedPagesSpecifyCache to ULONG_PTR.
Cant I straight away use that void pointer in the aplication side since I have declared it as PVOID in the application side.

when I use the PVOID straightly as I do for 32 bit machine I get error messages.

The actual guarantee and contract that I would allow for is:

You can assume that address space allocations in the user mode region of a 32-bit process that a driver (or 32-bit mode application code) makes can be safely truncated to 32-bit. That, or cases where you’re receiving pointers from 32-bit user mode where IoIs32bitProcess returns TRUE are the cases where I’d consider 32-bit truncation to be safe.

Please don’t truncate pointers outside of these cases however, as that gets into making assumptions about how Wow64 works and that’s not safe to do. (That being said, I cannot offhand think of a situation where you’d be typically dealing with a pointer from a 32-bit process which wasn’t provided in the cases listed above.)

Today, there is a single VAD above the highest 32-bit application address for a given process that helps to prevent stray allocations from going into that space:

fffffa80029a9010 ( 5) fffe0 7fffffef -1 Private READONLY

How this works is subject to change and it’s possible that there may one way be other reservations or allocations to support Wow64 one day. Unless you’re manually grunging around in the VAD tree or doing other undocumented and unsafe operations which you really shouldn’t be doing, you should be insulated from that by the above contract.

  • S

From: xxxxx@lists.osr.com [xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih [xxxxx@storagecraft.com]
Sent: Thursday, August 06, 2009 7:28 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] MmMapLockedPagesSpecifyCache doubt

Is it since the OS is a 32 bit one the address is always less than 2 GB???

On 32bit OS - yes (sometimes 3GB, sometimes 2GB)

For 32bit process in 64bit OS - too. Any functions which deal with user memory will never allocate/create a VAD which goes beyound the 32bit-accessible 4GB.

More so, skipping the LARGEADDRESSAWARE linker flag when building the EXE will force the 64bit kernel to place all VADs below 3GB, even not 4GB.


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com


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