global kernel lock

Hello,

I have a small piece of code within my driver, which must not be
interrupted by any other thread.
My 1st thought was to use _asm cli, but I dont know if this will work on
multi processor systems
(or even p4 with hyperthreading) as well.

Is there an API call to prevent interruption?
Or what is the proper way to achieve this.

Best Regards
Michael

Michael,

Sorry to have to ask, but: Why?

Unless you’re building a debugger or some such, there’s really no valid case
for “Must not be interrrupt by another thread”. There’s probably something
wrong in your design if “must” do this. You should ever only need to block
threads that are accessing the same data as your own thread.

However, if you really want to do BAD design in your driver, a KeRaiseIRQL()
call with sufficently high IRQL will do the job. Obviously followed by
KeLowerIRQL(oldIRQL). [OldIRQL is passed back from the Raise function]

It’s EXTREMELY bad practice to do this, however.

You should never do this in a production driver.

If you explain what you’re trying to achieve, to the members of this mailing
list, I’m sure someone will suggest some suitable solution to the problem.

Further, unless this lock is really short, you’ll be in trouble with
Microsoft once they start using the “maximum interrupt latency” method to
achieve real time video and audio, which states that any driver must not
block interrupts for more than X microseconds per second. If you block all
interrupts and/or threads, you probably can’t fulfill this criteria, unless
it’s like three lines of code in between raise and lower IRQL.


Mats

-----Original Message-----
From: xxxxx@sonydadc.com [mailto:xxxxx@sonydadc.com]
Sent: Tuesday, January 13, 2004 4:05 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] global kernel lock

Hello,

I have a small piece of code within my driver, which must not be interrupted
by any other thread.
My 1st thought was to use _asm cli, but I dont know if this will work on
multi processor systems
(or even p4 with hyperthreading) as well.

Is there an API call to prevent interruption?
Or what is the proper way to achieve this.

Best Regards
Michael

— Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256 You are currently subscribed to
ntdev as: xxxxx@3dlabs.com To unsubscribe send a blank email to
xxxxx@lists.osr.com

A bad design by any other name is still a bad design. Besides the assembly sequence “pusfd/cli/…/popfd” cannot absolutely prevent pre-emption, nor has it ever, even in the glory days of DOS. It does not prevent NMI from interrupting the system. You can raise IRQL to an arbitrary high level, but about the only way you can your device will not be interrupted is to tie your device to NMI … and then you have no support APIs to assist you in hooking your interrupt nor syncing access to your code.


Gary G. Little
Seagate Technologies, LLC

wrote in message news:xxxxx@ntdev…
Michael,

Sorry to have to ask, but: Why?

Unless you’re building a debugger or some such, there’s really no valid case for “Must not be interrrupt by another thread”. There’s probably something wrong in your design if “must” do this. You should ever only need to block threads that are accessing the same data as your own thread.

However, if you really want to do BAD design in your driver, a KeRaiseIRQL() call with sufficently high IRQL will do the job. Obviously followed by KeLowerIRQL(oldIRQL). [OldIRQL is passed back from the Raise function]

It’s EXTREMELY bad practice to do this, however.

You should never do this in a production driver.

If you explain what you’re trying to achieve, to the members of this mailing list, I’m sure someone will suggest some suitable solution to the problem.

Further, unless this lock is really short, you’ll be in trouble with Microsoft once they start using the “maximum interrupt latency” method to achieve real time video and audio, which states that any driver must not block interrupts for more than X microseconds per second. If you block all interrupts and/or threads, you probably can’t fulfill this criteria, unless it’s like three lines of code in between raise and lower IRQL.


Mats
-----Original Message-----
From: xxxxx@sonydadc.com [mailto:xxxxx@sonydadc.com]
Sent: Tuesday, January 13, 2004 4:05 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] global kernel lock

Hello,

I have a small piece of code within my driver, which must not be interrupted by any other thread.
My 1st thought was to use _asm cli, but I dont know if this will work on multi processor systems
(or even p4 with hyperthreading) as well.

Is there an API call to prevent interruption?
Or what is the proper way to achieve this.

Best Regards
Michael

— Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You are currently subscribed to ntdev as: xxxxx@3dlabs.com To unsubscribe send a blank email to xxxxx@lists.osr.com

For what? Why such a requirement?

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

----- Original Message -----
From: xxxxx@sonydadc.com
To: Windows System Software Devs Interest List
Sent: Tuesday, January 13, 2004 7:05 PM
Subject: [ntdev] global kernel lock

Hello,

I have a small piece of code within my driver, which must not be interrupted by any other thread.
My 1st thought was to use _asm cli, but I dont know if this will work on multi processor systems
(or even p4 with hyperthreading) as well.

Is there an API call to prevent interruption?
Or what is the proper way to achieve this.

Best Regards
Michael

— Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256 You are currently subscribed to ntdev as: xxxxx@storagecraft.com To unsubscribe send a blank email to xxxxx@lists.osr.com

Hi michael,

Use Spin Locks, and dont make ur kernel application platform dependent. Spin locks are there to use among the multiple processors, it will solve the problem. Before getting in the code take the spin lock and while coming out side u need to release, make sure that every exiting path shud realease the spin locks.

 

Good Luck,



From: xxxxx@sonydadc.com

Reply-To: “Windows System Software Devs Interest List”

>To: “Windows System Software Devs Interest List”
>Subject: [ntdev] global kernel lock
>Date: Tue, 13 Jan 2004 17:05:24 +0100
>
>Hello,
>
>I have a small piece of code within my driver, which must not be
>interrupted by any other thread.
>My 1st thought was to use _asm cli, but I dont know if this will work on
>multi processor systems
>(or even p4 with hyperthreading) as well.
>
>Is there an API call to prevent interruption?
>Or what is the proper way to achieve this.
>
>
>
>Best Regards
>Michael
>
>
>
>—
>Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>To unsubscribe send a blank email to xxxxx@lists.osr.com


Marriage? Join BharatMatrimony.com and get married.

Mats,

Thanks for your reply.

Sorry to have to ask, but: Why?
Unless you’re building a debugger or some such, there’s really no valid
case for “Must not be interrrupt by another thread”. There’s probably
something wrong in your design if “must” >do this. You should ever only
need to block threads that are accessing the same data as your own thread.

With the debugger you are not far away from the truth. I want to hook
kernel API functions, by overwriting the 1st 5 byte of the hooked function
with a “jmp xxx”. And as such a jmp instructions is 5 byte long, it can
not be written within one intruction. (My 1st idea was simply using a lock
prefix )
So I need to prevent others from calling the API function during Iam
writing the “jmp xxx”.

(In the old WIn9x days I could simply use a kernel service hook, and I did
not find a similar solution for 2K, XP)

However, if you really want to do BAD design in your driver, a
KeRaiseIRQL() call with sufficently high IRQL will do the job. Obviously
followed by KeLowerIRQL(oldIRQL). [ OldIRQL is >passed back from the
Raise function]
It’s EXTREMELY bad practice to do this, however.
>You should never do this in a production driver.

Best Regards
Michael

Ok, so here’s ONE idea.

Read 8 bytes at the address you’re modifying, using an MMX register. Then
write mask out the first 5 bytes, and modify. Then store the value.

Using an MMX register should make it a “singe write operation”. If the
address is aligned to 8 bytes it should not be a problem to make the bus
transaction atomic. If the address isn’t aligned to 8 bytes, there’s little
you can do about it.

Note of course that you need to save FPU contents using the
“KeSaveFloatingPointState” and restore again with
“KeRestoreFloatingPointState”.


Mats

-----Original Message-----
From: xxxxx@sonydadc.com [mailto:xxxxx@sonydadc.com]
Sent: Wednesday, January 14, 2004 7:45 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] global kernel lock

Mats,

Thanks for your reply.

Sorry to have to ask, but: Why?
Unless you’re building a debugger or some such, there’s really no valid
case for “Must not be interrrupt by another thread”. There’s probably
something wrong in your design if “must” >do this. You should ever only need
to block threads that are accessing the same data as your own thread.

With the debugger you are not far away from the truth. I want to hook kernel
API functions, by overwriting the 1st 5 byte of the hooked function with a
“jmp xxx”. And as such a jmp instructions is 5 byte long, it can not be
written within one intruction. (My 1st idea was simply using a lock prefix )

So I need to prevent others from calling the API function during Iam writing
the “jmp xxx”.

(In the old WIn9x days I could simply use a kernel service hook, and I did
not find a similar solution for 2K, XP)

However, if you really want to do BAD design in your driver, a
KeRaiseIRQL() call with sufficently high IRQL will do the job. Obviously
followed by KeLowerIRQL(oldIRQL). [ OldIRQL is >passed back from the Raise
function]
It’s EXTREMELY bad practice to do this, however.
>You should never do this in a production driver.

Best Regards
Michael — Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256 You are currently subscribed to
ntdev as: xxxxx@3dlabs.com To unsubscribe send a blank email to
xxxxx@lists.osr.com