Safest way to pause in a KMDF driver?

Ok, I have a nice hardware device - I write to a register to reset the device, and I have to wait until I read a specific value back out before I can proceed to the next operation.

So, what’s the safest way to do this? It’s pefectly possible for there to be a hardware issue and have the device never come out of reset, so just spinning in a loop is unwise. Putting a counter inside the loop is lame, because depending on processor speed the counter won’t be an accurate measure of time (it’s allowed to take 10msec to reset). And, I’m pretty sure that calling sleep() in a kernel driver is a Bad Thing™.

So, is there some safe way to waste time in a kernel mode driver? If there’s a clock accessible from kernel mode I can use that, but my Google-Fu is weak today and nothing pertinent popped up.

All help, URLs, etc. appreciated!

…ed…

sleep() kernel equivalent is KeDelayExecutionThread. That assumes that you’re in a context that allows for waiting (on PASSIVE_LEVEL).

xxxxx@woolyloach.com wrote:

Ok, I have a nice hardware device - I write to a register to reset the device, and I have to wait until I read a specific value back out before I can proceed to the next operation.

So, what’s the safest way to do this? It’s pefectly possible for there to be a hardware issue and have the device never come out of reset, so just spinning in a loop is unwise. Putting a counter inside the loop is lame, because depending on processor speed the counter won’t be an accurate measure of time (it’s allowed to take 10msec to reset). And, I’m pretty sure that calling sleep() in a kernel driver is a Bad Thing™.

Assuming this is in response to a user-mode request, where you are
allowed to block, there’s nothing wrong with a loop as long as there is
an escape hatch:

LARGE_INTEGER li;
li.QuadPart = -10000; // 1 ms delay
int i;
for( i = 0; i < 100; i++ )
{
if( READ_REGISTER_ULONG(addr) & RESET_COMPLETE )
break;
KeDelayExecutionThread( KernelMode, FALSE, &li );
}
if( i == 100 )
{
// Device did not respond in 100ms.
}


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

In addition to a passive level KeDelayExecutionThread you can schedule timer
based callbacks to run periodically. For hardware devices this is generally
a good thing to always have available to do background checks on hardware
status and you could easily stage your reset processing into it.

Mark Roddy

On Tue, Sep 28, 2010 at 2:43 PM, wrote:

> Ok, I have a nice hardware device - I write to a register to reset the
> device, and I have to wait until I read a specific value back out before I
> can proceed to the next operation.
>
> So, what’s the safest way to do this? It’s pefectly possible for there to
> be a hardware issue and have the device never come out of reset, so just
> spinning in a loop is unwise. Putting a counter inside the loop is lame,
> because depending on processor speed the counter won’t be an accurate
> measure of time (it’s allowed to take 10msec to reset). And, I’m pretty
> sure that calling sleep() in a kernel driver is a Bad Thing™.
>
> So, is there some safe way to waste time in a kernel mode driver? If
> there’s a clock accessible from kernel mode I can use that, but my Google-Fu
> is weak today and nothing pertinent popped up.
>
> All help, URLs, etc. appreciated!
>
> …ed…
>
>
> —
> 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
>

There is also KeSynchronizeExecution which will allow the OP to wait a
specified # of microseconds.

Gary G. Little
H (952) 223-1349
C (952) 454-4629
xxxxx@comcast.net

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@broadcom.com
Sent: Tuesday, September 28, 2010 12:46 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Safest way to pause in a KMDF driver?

sleep() kernel equivalent is KeDelayExecutionThread. That assumes that
you’re in a context that allows for waiting (on PASSIVE_LEVEL).


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

Lots of good info here - thanks to you all! Off to write code and research timer callbacks.

…ed…

Gary G. Little wrote:

There is also KeSynchronizeExecution which will allow the OP to wait a
specified # of microseconds.

I think you meant KeStallExecutionProcessor. KeSynchronizeExecution has
a rather different purpose.

KeStallExecutionProcessor does allow finer grained delays, but you need
to remember that it works by using a tight CPU loop, using up 100% of
the CPU. It should be applied sparingly.


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

Yes, seems to have been a bad case of flatulent memory … I do mean
KeStallExecutionProcessor.

Gary G. Little
H (952) 223-1349
C (952) 454-4629
xxxxx@comcast.net

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Tim Roberts
Sent: Tuesday, September 28, 2010 1:04 PM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] Safest way to pause in a KMDF driver?

Gary G. Little wrote:

There is also KeSynchronizeExecution which will allow the OP to wait a
specified # of microseconds.

I think you meant KeStallExecutionProcessor. KeSynchronizeExecution has a
rather different purpose.

KeStallExecutionProcessor does allow finer grained delays, but you need to
remember that it works by using a tight CPU loop, using up 100% of the CPU.
It should be applied sparingly.


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


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