From what I understand the ‘dispatcher’ runs when the IRQL drops from
DISPATCH_LEVEL or higher to PASSIVE_LEVEL or APC_LEVEL. The
‘dispatcher’ then picks a thread that is ready to run and is the highest
priority and oldest within identical priorities (rough description).
Calling one of the ‘wait’ requests, other than KeDelayExecutionThread()
has a thread ‘wait’ on some activity by another thread. Many IRPs and
system calls require the ability to wait for something to happen. So if
you call one of these at DISPATCH_LEVEL the wait cannot be done since
the thread that will satisfy the wait condition cannot be run by the
‘dispatcher’. I think the triple use of ‘dispatch’ is a problem when
trying to understand how the system works: 1) dispatch routine(s) in
drivers that process various IRPs, 2) the OS ‘dispatcher’ logic that
runs the thread that should be run next, 3) IRQL of DISPATCH_LEVEL. You
would almost think from the names that they are related, but only 2 & 3
have any relationship. One way to think about DISPATCH_LEVEL is that it
is really DONT_DO_DISPATCHING_OF_THREADS_LEVEL.
You cannot yield when at DISPATCH_LEVEL or higher and even APC_LEVEL has
some problems associated with it if you are trying to do some I/O such
as reading or writing to the hard drive. The correct way to handle
those cases is to schedule a ‘worker thread’ to process the request
from that point onward. With some of the power and PnP requests, even
that is not acceptable all the time. Read and understand Walter Oney’s
WDM 2nd Edition book and then this becomes easier. DriverWorks has a
good framework that can help someone write a driver, but it can have
problems if you don’t understand how the system works since you won’t
know what the limits may be for non-standard activities. The new
framework Microsoft is implementing should be another way to help
beginners and even more experienced programmers get it correct, but I
think you will not be able to do things ‘outside the box’ unless you
understand how it all fits together. Documenting the things that can
and should be done in each piece of the driver’s code will be critical
to helping us get it correct, but as a general rule we all seem to avoid
the docs until we have problems.
It took Walter a long time to get his power/PnP logic working and he
provides it in a driver DLL so you can just use it or you can
incorporate the code within your driver if you prefer. Even Microsoft
has most of the PnP & power logic for DISK.SYS in the CLASSPNP.SYS
driver. It also provides these services to other drivers in the same
storage ‘class’ including tape and cdrom.
PC/SC uses SMCLIB.SYS as a driver DLL/miniport to keep SmartCard request
much simpler than if you had to implement the full ISO spec Too bad
that after all that work on PC/SC so little use is made of it in the
U.S.
“Ivan Bublikov” wrote in message
news:xxxxx@ntdev…
> Yes, the big picture of DISPATCH_LEVEL seems to be that whoever raises
the
> IRQL to that level effectively tells everybody (whoever he is going to
call
> afterwards, or the system dispatcher, or the virtual memory page-fault
> handler) that he can’t tolerate yielding the CPU to another thread. As
a
> consequence, if he mistakenly invokes some code that knows about
itself that
> it can yield, the code (if it is courteous enough) will immediately
point
> that out by bugchecking.
>
> Therefore, if it was you who raised to DISPATCH_LEVEL and you now want
to
> yield, just drop it back and then yield. If it was not you, but some
other
> guy you don’t know called you at DISPATCH_LEVEL, you can not yield,
because
> you would let that guy down.
>
> “Moreira, Alberto” wrote in message
> news:xxxxx@ntdev…
> > I don’t know. It looks to me like while a thread running at dispatch
level
> > cannot be preempted by anyone running at a lower level, that doesn’t
mean
> it
> > cannot voluntarily yield control by calling KeWaitForSingleObject:
when
> that
> > happens, the thread effectively leaves the ready state, and other
threads
> > will be running normally, even in the same processor. When the
object is
> > released, the dispatch level thread will get control back, and then
it
> won’t
> > be preemptable by anyone running at lower levels, but still, if it
again
> > waits on a busy mutex, it’s going to release control.
> >
> > Unless of course I misunderstand the nature of the WinNT dispatcher
?
> Which
> > is quite possible, every once in a while I learn something new about
the
> > system from NTDEV posters !
> >
> > Alberto.
> >
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com]On Behalf Of Peter Wieland
> > Sent: Thursday, March 11, 2004 12:04 PM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] using KeWaitForxxx properly with verifier
> >
> >
> > sorry
> >
> > add “at dispatch level” after “and is indeed invalid”.
> >
> > -p
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com] On Behalf Of Moreira,
Alberto
> > Sent: Thursday, March 11, 2004 8:30 AM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] using KeWaitForxxx properly with verifier
> >
> > From the doc:
> >
> > ===================
> > If no Timeout is supplied, the calling thread remains in a wait
state
> > until the Object is signaled.
> >
> > A Timeout of zero allows the testing of a set of wait conditions and
for
> > the conditional performance of any side effects if the wait can be
> > immediately satisfied, as in the acquisition of a mutex.
> > ===================
> >
> > The parameter is optional, hence it should probably be quite ok to
pass
> > a NULL value on that pointer ? And notice the semantic difference
> > between no timeout and a timeout of zero.
> >
> > Alberto.
> >
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com]On Behalf Of Peter Wieland
> > Sent: Thursday, March 11, 2004 11:23 AM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] using KeWaitForxxx properly with verifier
> >
> >
> > No, that wouldn’t make sense. That’s also not what the OP wrote.
> >
> > He wrote that passing a NULL pointer to the timeout value at
dispatch
> > level caused the verifier to raise an error. This is specifying no
> > timeout (not zero timeout) and is indeed invalid.
> >
> > If you want to do a zero timeout you need to pass in a pointer to a
> > LARGE_INTEGER value that contains 0.
> >
> > -p
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com] On Behalf Of Prokash Sinha
> > Sent: Thursday, March 11, 2004 8:00 AM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] using KeWaitForxxx properly with verifier
> >
> > Does it really make sense to you, that passing zero wait should
cause
> > driver verifier to raise a flag ?
> >
> > Not to me !
> >
> > -prokash
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com]On Behalf Of Peter Wieland
> > Sent: Thursday, March 11, 2004 7:45 AM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] using KeWaitForxxx properly with verifier
> >
> >
> > Huh?
> >
> > Driver Verifier does NO static analysis. It consist of extra
checking
> > code in the implementations of the WDM DDIs.
> >
> > -p
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com] On Behalf Of Prokash Sinha
> > Sent: Thursday, March 11, 2004 7:25 AM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] using KeWaitForxxx properly with verifier
> >
> > I might be wrong !. But how does it do it ? My guess would be that
it is
> > another instrumentation. Now w/o looking at the DDK header file, it
> > sounds like that KeWait*() is not a macro ( for example KdPrint()
is ),
> > if it were a macro, and it dispatches to different real kernel
routines
> > (including possibly null routine) based on the value of the
variable,
> > then instrumentation takes only to the functions that are just
supposed
> > to work at below DISPATCH level …
> >
> > -prokash
> >
> >
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com]On Behalf Of Don Burn
> > Sent: Thursday, March 11, 2004 7:09 AM
> > To: Windows System Software Devs Interest List
> > Subject: Re:[ntdev] using KeWaitForxxx properly with verifier
> >
> >
> > Well I’m not sure where the “static analysis” comes in, since driver
> > verifier is a set of addtional run-time checking built into the
system.
> >
> > –
> > Don Burn (MVP, Windows DDK)
> > Windows 2k/XP/2k3 Filesystem and Driver Consulting Remove StopSpam
from
> > the email to reply
> >
> > “Prokash Sinha” wrote in message
> > news:xxxxx@ntdev…
> > > This sounds like a bonafied bug in a static analysis !!!
> > >
> > > What could the DV do, if I declare a variable, and set it to zero.
It
> > would
> > > have to ply thru and find the value of a variable, if zero then
let it
> > pass,
> > > otherwise raise a flag.
> > >
> > > This variable’s value can change anywhere !. So it is tough here !
> > >
> > > -prokash
> > >
> > > -----Original Message-----
> > > From: xxxxx@lists.osr.com
> > > [mailto:xxxxx@lists.osr.com]On Behalf Of Daniel
Terhell
> > > Sent: Thursday, March 11, 2004 4:02 AM
> > > To: Windows System Software Devs Interest List
> > > Subject: [ntdev] using KeWaitForxxx properly with verifier
> > >
> > >
> > > Hi,
> > >
> > > In my driver I have to acquire a mutex in a routine which gets
called
> > both
> > > at PASSIVE_LEVEL as well as DISPATCH_LEVEL from a DPC routine.
> > > The DDK doc says that it is okay to use KeWaitForMutexObject or
> > > KeWaitForSingleObject at DISPATCH_LEVEL if a timeout of 0 is
> > specified.
> > >
> > > This goes fine, however when I put on Driver Verifier, I have to
alter
> > my
> > > code to support the different IRQLs. I’m now acquiring my mutex in
the
> >
> > > following routine:
> > >
> > > void WaitForMutex(BOOLEAN fromDPC)
> > > {
> > > LARGE_INTEGER timeOut={0,0},*t;
> > > if (fromDPC==TRUE) t=&timeOut; else t=NULL;
> > > KeWaitForSingleObject(&MyMutex,Executive, KernelMode, FALSE,
t); }
> > >
> > > It appears that the Driver Verifier won’t accept a timeout of NULL
at
> > > DISPATCH_LEVEL, but insists on an initialized variable containing
0
> > instead.
> > >
> > > But this is not where the problems finishes. If I put on deadlock
> > detection
> > > in the verifier, these KeWaitForxxx functions sometimes fail just
for
> > the
> > > fun of it, then the blue screen comes when trying to release a
mutex
> > which
> > > was never acquired. This means I have to check the return values
of
> > these
> > > KeWaitForxxx functions. However if I check the sample code from
the
> > DDK or
> > > drivers written by the gurus they never check these return values.
> > >
> > > The question is whether it is not silly to alter and compromise
code
> > which
> > > is otherwise running perfectly well only to satisfy the verifier ?
> > >
> > >
> > > Thanks,
> > >
> > > Daniel in Resplendence
> > >
> > >
> > >
> > >
> > >
> > > —
> > > Questions? First check the Kernel Driver FAQ at
> > > http://www.osronline.com/article.cfm?id=256
> > >
> > > You are currently subscribed to ntdev as: xxxxx@garlic.com To
> > > unsubscribe send a blank email to xxxxx@lists.osr.com
> > >
> > >
> > >
> > >
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@garlic.com To
> > unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as:
xxxxx@windows.microsoft.com
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@garlic.com To
> > unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as:
xxxxx@windows.microsoft.com
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as:
xxxxx@compuware.com
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> >
> >
> > The contents of this e-mail are intended for the named addressee
only.
> > It contains information that may be confidential. Unless you are the
> > named addressee or an authorized designee, you may not copy or use
it,
> > or disclose it to anyone else. If you received it in error please
notify
> > us immediately and then destroy it.
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as:
xxxxx@windows.microsoft.com
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as:
xxxxx@compuware.com
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> >
> >
> > The contents of this e-mail are intended for the named addressee
only. It
> > contains information that may be confidential. Unless you are the
named
> > addressee or an authorized designee, you may not copy or use it, or
> disclose
> > it to anyone else. If you received it in error please notify us
> immediately
> > and then destroy it.
> >
> >
>
>