陷阱分發:
陷阱(trap)指的是異常或中斷髮生時處理器撲捉到一個執行線程,並將控制權轉移到操作系統某一固定地址處。windows中控制權轉移給一個
陷阱處理器(trap handler),與某個異常或中斷相關聯的函數。
中斷:是一個異步事件,可以在任何時候發生,與處理器當前執行的任務無關,
異常:同步條件,一個特殊指令執行結過。
如果中斷的線程是在用戶模式下運行,windows就切換該線程到內核模式棧,wondows在被中斷的線程的內核棧上創建一個陷阱幀(trap frame
),並把線程的執行狀態保存到陷阱幀裏。
陷阱幀是一個線程完整指行環境的一個子集,調試中輸入
dt nt!_ktrap_frame
可以看出結構定義:
如下:
nt!_KTRAP_FRAME
+0x000 DbgEbp : Uint4B
+0x004 DbgEip : Uint4B
+0x008 DbgArgMark : Uint4B
+0x00c DbgArgPointer : Uint4B
+0x010 TempSegCs : Uint4B
+0x014 TempEsp : Uint4B
+0x018 Dr0 : Uint4B ;幾個調試寄存器
+0x01c Dr1 : Uint4B
+0x020 Dr2 : Uint4B
+0x024 Dr3 : Uint4B
+0x028 Dr6 : Uint4B
+0x02c Dr7 : Uint4B
+0x030 SegGs : Uint4B
+0x034 SegEs : Uint4B
+0x038 SegDs : Uint4B
+0x03c Edx : Uint4B
+0x040 Ecx : Uint4B
+0x044 Eax : Uint4B
+0x048 PreviousPreviousMode : Uint4B
+0x04c ExceptionList : Ptr32 _EXCEPTION_REGISTRATION_RECORD ;指向異常鏈表
+0x050 SegFs : Uint4B
+0x054 Edi : Uint4B
+0x058 Esi : Uint4B
+0x05c Ebx : Uint4B
+0x060 Ebp : Uint4B
+0x064 ErrCode : Uint4B
+0x068 Eip : Uint4B
+0x06c SegCs : Uint4B
+0x070 EFlags : Uint4B
+0x074 HardwareEsp : Uint4B
+0x078 HardwareSegSs : Uint4B
+0x07c V86Es : Uint4B
+0x080 V86Ds : Uint4B
+0x084 V86Fs : Uint4B
+0x088 V86Gs : Uint4B
保存各寄存器狀態。
在系統引導的時候,windows會填充IDT(interrupt descript table),其中包含了指向內核中負責處理每個中斷和異常的例程指針。
利用!idt可以看到IDT的內容。
lkd> !idt
Dumping IDT:
37: 806e4864
3d: 806e5e2c
41: 806e5c88
50: 806e493c
62: 89c97954 ba6105e0 (KINTERRUPT 89c97918)
63: 8970b044 ba59ce80 (KINTERRUPT 8970b008)
b82bfbca (KINTERRUPT 896f6bb0)
73: 89a7cbec b82deb78 (KINTERRUPT 89a7cbb0)
b82bfbca (KINTERRUPT 896febb0)
b82bfbca (KINTERRUPT 896f7bb0)
82: 89cba954 ba6105e0 (KINTERRUPT 89cba918)
83: 89c86afc ba6105e0 (KINTERRUPT 89c86ac0)
ba6105e0 (KINTERRUPT 89cb2bb0)
92: 896f8634 ba958a30 (KINTERRUPT 896f85f8)
93: 89c3176c ba968495 (KINTERRUPT 89c31730)
a3: 89c95bec ba96fd80 (KINTERRUPT 89c95bb0)
a4: 89702044 b82bfbca (KINTERRUPT 89702008)
b82bfbca (KINTERRUPT 896f8bb0)
b1: 89ce32cc ba68431e (KINTERRUPT 89ce3290)
ba6a9da8 (KINTERRUPT 896f8008)
b4: 89709bec b82bfbca (KINTERRUPT 89709bb0)
b825fbd8 (KINTERRUPT 896f0bb0)
c1: 806e4ac0
d1: 806e3e54
e1: 806e5048
e3: 806e4dac
fd: 806e55a8
顯示中斷號,及中斷ISR(interrupt server routine)地址。