Some basic questions about driver programming

If I am writing a driver in C and I create a global variable, for example
just before my DriverEntry declaration, is that variable going to be in
non-paged pool or paged-pool memory by default?

In WDM, it has become apparent to me that a device isn’t necessarily either
completely disabled nor removed when a call to IoDeleteDevice returns, so
what is the proper method of finding out when its actually gone?

Non paged unless you explicitly change something.

I don’t know a damn thing about WDM.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Matthew Carter
Sent: Tuesday, June 26, 2007 18:14
To: Windows System Software Devs Interest List
Subject: [ntdev] Some basic questions about driver programming

If I am writing a driver in C and I create a global variable, for
example
just before my DriverEntry declaration, is that variable going to be in
non-paged pool or paged-pool memory by default?

In WDM, it has become apparent to me that a device isn’t necessarily
either
completely disabled nor removed when a call to IoDeleteDevice returns,
so
what is the proper method of finding out when its actually gone?


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

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

Global data is by default non-paged.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr
Remove StopSpam to reply

“Matthew Carter” wrote in message
news:xxxxx@ntdev…
> If I am writing a driver in C and I create a global variable, for example
> just before my DriverEntry declaration, is that variable going to be in
> non-paged pool or paged-pool memory by default?
>
> In WDM, it has become apparent to me that a device isn’t necessarily
> either completely disabled nor removed when a call to IoDeleteDevice
> returns, so what is the proper method of finding out when its actually
> gone?
>
>

> If I am writing a driver in C and I create a global variable, for example

just before my DriverEntry declaration, is that variable going to be in
non-paged pool or paged-pool memory by default?

Neither of the above…

Your global variable is going to reside in non-pageable memory by default, but it has nothing to do with non-paged pool, which is used for dynamic memory allocations…

Anton Bassov

Matthew Carter wrote:

If I am writing a driver in C and I create a global variable, for example
just before my DriverEntry declaration, is that variable going to be in
non-paged pool or paged-pool memory by default?

If you haven’t issued a #pragma to override it, it will be in non-paged
pool.

In WDM, it has become apparent to me that a device isn’t necessarily either
completely disabled nor removed when a call to IoDeleteDevice returns, so
what is the proper method of finding out when its actually gone?

What do you mean by “gone”? When you do IoDeleteDevice, the device
object is removed, so no one can submit requests to it anymore. The
driver will remain in memory until it is asked to unload, which will be
shortly after the last device it supports is deleted.


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

> If I am writing a driver in C and I create a global variable, for example

just before my DriverEntry declaration, is that variable going to be in
non-paged pool or paged-pool memory by default?

Non-paged.

Non-paged memory, not pool. Pools are for dynamic allocations only.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

>> In WDM, it has become apparent to me that a device isn’t necessarily either

> completely disabled nor removed when a call to IoDeleteDevice returns, so
> what is the proper method of finding out when its actually gone?

What do you mean by “gone”?
When you do IoDeleteDevice, the device object is removed, so no one can submit requests
to it anymore. The driver will remain in memory until it is asked to unload, which will be
shortly after the last device it supports is deleted.

Actually, this is not really the case. In order to understand it, consider the following scenario:

Driver X calls IoGetDeviceObjectPointer() and gets a pointer to device object that has been created
by driver Y. Driver Y is a legacy driver that provides its Unload() routine ( i.e. a kind of driver that can get stopped by SCM upon the user request), and its Unload() routine deletes the device object in question by IoDeleteDevice().

Let’s assume for a moment that your statement is true. In such case, if user stops driver Y service while driver X still keeps a pointer to its device object, the system will bluescreen the next time driver X tries to access the target device.

Therefore, in order to avoid the above scenario, IoDeleteDevice() actually deletes the device only if its reference count is zero ( in our example it is non-zero because IoGetDeviceObjectPointer() increments it). Otherwise, it just marks a device for deletion. In our example, both target device and driver Y’s image will stay resident in RAM until driver X calls ObDereferenceObject(). Please note that outstanding reference count just keeps objects in RAM - it does not prevent Unload() routine from being invoked

I think this is what the OP means here - he means devices with outstanding refcount that have been marked for deletion

Anton Bassov

Understood. I see that there is a fine distinction to be made there. The
main question was about the paged or non-paged status since I was thinking
about putting a spin lock in the global memory and if it was paged that
would not work, however the point about it not being “pool” may be useful to
know sometime in the future if that implies that it will not subtract from
the amount of pool memory available to drivers.

I noticed that some samples use fast mutexes in global memory and since they
operate at APC level whereas a spin lock uses dispatch level they might
still work in paged memory where a spin lock would not. I wasn’t sure if
that was because global memory was incapable of it or if that was a design
decision based on the suitability of a spin lock vs. a fast mutex for a
particular purpose. Since the global memory is non-paged there should be no
problem using spin locks there, so it must be the latter. Thank you all for
for your answers.

“Maxim S. Shatskih” wrote in message
news:xxxxx@ntdev…
>> If I am writing a driver in C and I create a global variable, for example
>> just before my DriverEntry declaration, is that variable going to be in
>> non-paged pool or paged-pool memory by default?
>
> Non-paged.
>
> Non-paged memory, not pool. Pools are for dynamic allocations only.
>
> –
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> xxxxx@storagecraft.com
> http://www.storagecraft.com
>
>

> Therefore, in order to avoid the above scenario, IoDeleteDevice() actually

deletes the device only if its reference count is zero ( in our example it
is non-zero because IoGetDeviceObjectPointer() increments it). Otherwise,
it just marks a device for deletion. In our example, both target device
and driver Y’s image will stay resident in RAM until driver X calls
ObDereferenceObject(). Please note that outstanding reference count just
keeps objects in RAM - it does not prevent Unload() routine from being
invoked

Yes, that’s what I meant. I am wanting to restart driver Y because a new
version of the .sys file has been installed on the system. However, driver
Y runs a legacy device whose object hasbeen deleted but at the time of
deletion still had some outstanding references (open handles to the device
held by user applications in my case) so it just sits in memory. At some
point it will actually be removed from memory when the user closes the
handles, and only then will it be possible to replace the driver in memory
with the new version because the unloading of the device is actually
controlled by the PnP manager which will not ask for an unload while the
legacy device still has handles open. Once that legacy device is done I can
have the PnP devices request a restart of the driver, but I’m not sure how
to find out when the legacy device is really gone and not just sitting there
in memory waiting on its references to release.

There is no way to know when the device is deleted with pending
references (and when the ref goes to zero). Since you control the app
and the app which is copying the new driver file, make sure they play
nice together and avoid this problem by having the app close its handles
down during upgrade.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Matthew Carter
Sent: Tuesday, June 26, 2007 7:34 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Some basic questions about driver programming

Therefore, in order to avoid the above scenario, IoDeleteDevice()
actually
deletes the device only if its reference count is zero ( in our
example it
is non-zero because IoGetDeviceObjectPointer() increments it).
Otherwise,
it just marks a device for deletion. In our example, both target
device
and driver Y’s image will stay resident in RAM until driver X calls
ObDereferenceObject(). Please note that outstanding reference count
just
keeps objects in RAM - it does not prevent Unload() routine from being

invoked

Yes, that’s what I meant. I am wanting to restart driver Y because a
new
version of the .sys file has been installed on the system. However,
driver
Y runs a legacy device whose object hasbeen deleted but at the time of
deletion still had some outstanding references (open handles to the
device
held by user applications in my case) so it just sits in memory. At
some
point it will actually be removed from memory when the user closes the
handles, and only then will it be possible to replace the driver in
memory
with the new version because the unloading of the device is actually
controlled by the PnP manager which will not ask for an unload while the

legacy device still has handles open. Once that legacy device is done I
can
have the PnP devices request a restart of the driver, but I’m not sure
how
to find out when the legacy device is really gone and not just sitting
there
in memory waiting on its references to release.


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

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