操作系統——中斷機制(trap, interrupts)

中斷描述符表(Interrupt Descriptor Table,IDT)將每個異常或中斷向量分別與它們的處理過程聯繫起來,IDT也是由8字節長描述符組成的一個數組。

IDT表可以存放在線性地址空間的任何地址,處理器使用IDTR寄存器來定位IDT表的位置。

IDTR寄存器中含有32位的基地址和16位的長度值。

LIDT和SIDT指令分別用於加載和保存IDTR寄存器的內容。

LIDT指令用於把內存中的限長值和基地址操作數加載到IDTR寄存器中。該指令僅能由當前特權級CPL是0的代碼執行,通常被用於創建IDT時的操作
系統初始化代碼中。
SIDT指令用於把IDTR中的基地址和限長內容複製到內存中。該指令可在任何特權
級上執行。

The x86 allows up to 256 different interrupt or exception entry points into the kernel, each with a different interrupt vector.A vector is a number between 0 and 255. An interrupt's vector is determined by the source of the interrupt: different devices, error conditions, and application requests to the kernel generate interrupts with different vectors. The CPU uses the vector as an index into the processor's interrupt descriptor table (IDT), which the kernel sets up in kernel-private memory, much like the GDT. From the appropriate entry in this table the processor loads:

  • the value to load into the instruction pointer (EIP) register, pointing to the kernel code designated to handle that type of exception.
  • the value to load into the code segment (CS) register, which includes in bits 0-1 the privilege level at which the exception handler is to run. 

中斷向量和中斷向量表
中斷向量:中斷處理程序的入口地址。其實
中斷向量表:中斷處理程序的入口地址的列表

中斷描述符表和中斷向量表的區別聯繫

在保護模式下,中斷向量表中的表項由8個字節組成,根據中斷號可以索引其在中斷向量表中的位置,在該模式下,中斷向量表也可稱爲中斷描述符表IDT.

表中的每個entry稱爲一個門描述(gate descriptor) ,通過它進入相應的處理程序。

中斷門(interrupt gate)分類:

中斷門(Interrupt gate)
其類型碼爲110,中斷門包含了一箇中斷或異常處理程序所在段的選擇符和段內偏移量。當控制權通過中斷門進入中斷處理程序時,處理器清IF標誌,即關中斷,以避免嵌套中斷的發生。中斷門中的DPL(Descriptor Privilege Level)爲0,因此,用戶態的進程不能訪問Intel的中斷門。所有的中斷處理程序都由中斷門激活,並全部限制在內核態。
· 陷阱門(Trap gate)
其類型碼爲111,與中斷門類似,其唯一的區別是,控制權通過陷阱門進入處理程序時維持IF標誌位不變,也就是說,不關中斷。
· 系統門(System gate)
這是Linux內核特別設置的,用來讓用戶態的進程訪問Intel的陷阱門,因此,門描述符的DPL爲3。通過系統門來激活4個Linux異常處理程序,它們的向量是3、4、5及128,也就是說,在用戶態下,可以使用int3、into、bound 及int0x80四條彙編指令
最後,在保護模式下,中斷描述符表在內存的位置不再限於從地址0開始的地方,而是可以放在內存的任何地方。爲此,CPU中增設了一箇中斷描述符表寄存器IDTR,用來存放中斷描述符表在內存的起始地址。中斷描述符表寄存器IDTR是一個48位的寄存器,其低16位保存中斷描述符表的大小,高32位保存IDT的基址.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章