📜 ⬆️ ⬇️

Interrupts for the smallest

Today we will talk about interruptions of x86 (-64) processors. More under the cut.
Interrupts are like a signal to the processor that it is necessary to interrupt the execution (they are therefore called interrupts) of the current code and urgently do what is indicated in the handler.

IDT


All interrupt handler addresses are stored in the IDT . This is a table that stores 256 (more or less, but most of the values ​​are simply ignored) cells (interrupt vectors) with the type and attributes of the interrupt, one simply zero value, the address of the interrupt handler and the code selector in GDT or LDT, which will use this interrupt vector. Now a little about the type and attributes.
Interrupt type and attributes occupy 8 bits. The first 4 bits are of the type:

Next are the attributes. The first attribute is 1 bit, which is set to 0 for interrupt gates and 1 to the rest. Next comes the descriptor privilege level - 2 bits, which sets the minimum privilege level for the interrupt call, and 1 bit, set to 0 for unused interrupts.
Now how the processor causes handlers.
Suppose that you called the instruction int 0 in assembler. This will signal the processor to cause an interrupt of 0, if possible. Here is a sequence of actions that occur during this.
  1. Search vector number 0 in IDT.
  2. Comparison of the level of privileges of the descriptor and the current level of privileges of the processor.
  3. If the current privilege level of the processor is lower than the privilege level of the descriptor, then simply cause a general protection error and not cause an interrupt.
  4. The return address, register (E) FLAGS and other information are saved.
  5. There is a transition to the address specified in the vector №0 IDT.
  6. After executing the handler, the iret instruction returns control to the interrupted code.


Exceptions


There are also interrupts that are generated by the processor itself under certain circumstances - exceptions . Here is a list of them with brief descriptions:

')

IRQ


There is a special type of interrupt - IRQ (Interrupt ReQuest) , or hardware interrupts, but for brevity I'll call them just IRQ. Technically, they are almost the same as any other interrupts, but they are not generated by the processor or the code itself, but by devices connected to the computer. For example, IRQ # 0 generates a PIT (timer with a programmable interval), IRQ 1 is generated when you press a key on the keyboard, and IRQ 12 - when operating with a PS / 2 mouse.

Software interrupts


There are also so-called software interrupts . They, as the name implies, the program should call itself - no one calls them for it. These are, for example, system calls in some systems. In Linux, for example, they hang on the vector 0x80. In many hobby OS, they also hang on the vector 0x80. Now, a little ad-libbing - I think that syscalls are made as interrupts because 1) they are so very easy to call, 2) they can be called from any code running in the OS — the IDT is one for the whole system.
Info taken from OSDev Wiki .

Source: https://habr.com/ru/post/279627/


All Articles