CLI (x86 instruction)

From Free net encyclopedia

Revision as of 22:51, 17 April 2006; view current revision
←Older revision | Newer revision→

In the x86 assembly language, the CLI instruction is a mnemonic for CLear Interrupts. It clears the interrupt flag (IF) in the EFLAGS register (sets it to zero), which disables all maskable interrupts (but not non-maskable interrupts). The STI instruction is the inverse of the CLI instruction.

Overview

The CLI instruction is a privileged instruction, which triggers a general protection fault if an unprivileged application attempts to execute it. The privilege level required to execute a CLI (or STI) instruction is determined by the IOPL (I/O Privilege Level) in EFLAGS. If the IOPL is set to 2 for example, any program running in ring 0, 1, or 2 can execute a CLI. Most modern operating systems set the IOPL to be 0 so only the kernel can execute CLI/STI. The reason for this is that since CLI will force the processor to ignore ALL interrupts, the kernel may never get control back if an STI is not executed.

Note that the IF flag can also be changed by loading EFLAGS if the current privilege level is numerically equal to or lower than the (old value) of the IOPL.

CLI is commonly used as a synchronization mechanism in uniprocessor systems. For example, a CLI is used in operating systems to disable interrupts so kernel code (typically a driver) can avoid race conditions with an interrupt handler. Note that CLI only affects the interrupt flag for the processor on which it is executed; in multiprocessor systems, executing a CLI instruction does not disable interrupts on other processors. Thus, a driver/interrupt handler race condition can still occur because other processors may service interrupts and execute the offending interrupt handler. For these systems, other synchronization mechanisms such as locks must be used in addition to CLI/STI to prevent all race conditions.

Software interrupts, generated by the INT instruction, will be serviced irrespective of the value of IF.

Because the HLT instruction halts until an interrupt occurs, the combination of a CLI followed by a HLT is commonly used to intentionally hang the computer.

See also