Disable Hardware & Software Interrupts
Asked Answered
C

3

9

Is it possible to disable all interrupts with a ASM/C/C++ program to get full control about the processor?

If yes -> how?

If not -> how do "atomic" operation system calls work (for example entering a critical section)?

Thanks for your help!

Casual answered 6/10, 2009 at 18:35 Comment(3)
You need to state which processor you're using. And which OS if any. On modern desktop/server operating system, userland code usually runs in an unprivileged mode preventing you from doing this.Tirrell
MS Windows (XP SP2/SP3, Vista, 7) x86 and x64 versions, modern intel processors (e.g. Intel Core 2 Duo)Casual
re: atomic operations: Can num++ be atomic for 'int num'? describes how/why lock cmpxchg [rdi], ecx is atomic. Disabling interrupts on the current core would be useless anyway on a multi-core machine; even disabling interrupts on all cores wouldn't work if another core is already running another thread of the same program.Superelevation
K
12

In x86 assembly the the commands are

  • sti set interrupt enable bit
  • cli clear interrupt enable bit

These commands set and clear the IF Flag. When the IF flag is set, the CPU will handle hardware interrupts, and when it is clear the CPU will ignore hardware interrupts. It does not affect the handling of non-maskable interrupts though, nor does it affect software interrupts or exceptions. These instructions also don't work in unprivileged mode (usually everything higher than ring 0, depending on IOPL) though.

Keifer answered 17/10, 2009 at 8:21 Comment(3)
you got it backwards. STI enables interrupts. CLI disables.Poll
It also does not disable software interrupts or exceptionsRivy
Thank you guys for your comments and edits: STI enables interrupts, CLI disables interrupts is indeed correct. I was sure it was the other way round, but I learned my assembly on a C64, so I looked it up: The 6510 had CLI do disable and STI to enable. I'm confused;-)Keifer
R
3

on x86 and most other modern processors you can get atomic instructions. Ones that are GURANTEED not to be finished executing before another thread/processor can access that memory.

Under Win32 you have the Interlocked* functions that abstract that from you on supported platforms.

On a MIPS a lot of instruction can have a .I added to the end of the instruction to guarantee interlocking.

Refractory answered 6/10, 2009 at 18:39 Comment(2)
On a x86 you can prefix certain instructions with "lock" to make them atomic.Danieu
For what platform? If its for windows then msdn.microsoft.com/en-us/library/ms684122(VS.85).aspxRefractory
O
2

The x86 has an interrupt flag (IF) in the FLAGS register. When this flag is set to 0, hardware interrupts are disabled, otherwise they are enabled. The command cli sets this flag to 0, and sti sets it to 1. Instructions that load values into the FLAGS register (such as popf and iret) may also modify this flag.

good luck!

Ormond answered 5/3, 2013 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.