Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we need to disable all interrupts at system startup or system initialization?

What is need for disabling all interrupts at system initialization or at startup code level . If I do not disable the interrupts what will happen??

like image 513
Raj Rajesh Avatar asked Jan 22 '16 13:01

Raj Rajesh


1 Answers

There are certain situations interrupts are unwanted, so they are disabled.
The examples are numerous but off the top of my head I can come up with these:

  • Modification of ss:(e)sp. If an interrupt is fired, the flags register is pushed onto the stack. An invalid stack value will move that copy to some random location. Alterating ss:(e)sp is not atomic, at least on x86, because it consists of multiple instructions, so an interrupt can fire in between.
    However, if you write your code properly, you can achieve the same atomicity without disabling interrupts here because they are automatically disabled on certain occasions.

    @MichaelPetch uttered some specialty about 8088 processors (the "weaker brother" of the 8086, the first x86 processor), depicting an exception to these "certain occasions," in the comments to this answer:

    That is true about interrupts being turned off until the end of the next instruction (after moving a value into SS ), but there were 8088 processors with a bug where the interrupts weren't properly turned off after a SS change. Those of us (the dinosaurs) will often put CLI/STI around the SS:SP update just in case (Chances of running an 8088 system with such a bug is likely near zero). From a historical perspective, this PC mag article may shed some light on this ancient issue.

    (Code formatters added.)

  • Lack of an IDT/IVT. While the Protected Mode IDT is initialized or the 16-bit Real Mode IVT is modified (or zeroed out or something), an interrupt would jump to some memory location no instruction is located at.

In general, you can say that operations modifying the IDT/IVT in some non-atomic manner need to disable interrupts.


As an aside: I myself have written several bootloaders already and usually disable interrupts throughout the whole runtime of the bootloader. In Protected Mode, I reenable them eventually. Linux 4.2 handles it similarly. If you're interested, read its source code (/arch/x86/boot/) or that from Minix!

like image 149
cadaniluk Avatar answered Nov 11 '22 08:11

cadaniluk