trap.s

来自「一个类似windows」· S 代码 · 共 1,043 行 · 第 1/2 页

S
1,043
字号
    iret

AbiosExit:
    /* Not yet supported */
    int 3

.func KiDebugService
_KiDebugService:

    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(kids)

    /* Increase EIP so we skip the INT3 */
    //inc dword ptr [ebp+KTRAP_FRAME_EIP]

    /* Call debug service dispatcher */
    mov eax, [ebp+KTRAP_FRAME_EAX]
    mov ecx, [ebp+KTRAP_FRAME_ECX]
    mov edx, [ebp+KTRAP_FRAME_EAX]

    /* Check for V86 mode */
    test dword ptr [ebp+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
    jnz NotUserMode

    /* Check if this is kernel or user-mode */
    test byte ptr [ebp+KTRAP_FRAME_CS], 1
    jz CallDispatch
    cmp word ptr [ebp+KTRAP_FRAME_CS], KGDT_R3_CODE + RPL_MASK
    jnz NotUserMode

    /* Re-enable interrupts */
VdmProc:
    sti

    /* Call the debug routine */
CallDispatch:
    mov esi, ecx
    mov edi, edx
    mov edx, eax
    mov ecx, 3
    push edi
    push esi
    push edx
    call _KdpServiceDispatcher@12

NotUserMode:

    /* Get the current process */
    mov ebx, [fs:KPCR_CURRENT_THREAD]
    mov ebx, [ebx+KTHREAD_APCSTATE_PROCESS]

    /* Check if this is a VDM Process */
    //cmp dword ptr [ebx+KPROCESS_VDM_OBJECTS], 0
    //jz VdmProc

    /* Exit through common routine */
    jmp _Kei386EoiHelper@0
.endfunc

.func NtRaiseException@12
_NtRaiseException@12:

    /* NOTE: We -must- be called by Zw* to have the right frame! */
    /* Push the stack frame */
    push ebp

    /* Get the current thread and restore its trap frame */
    mov ebx, [fs:KPCR_CURRENT_THREAD]
    mov edx, [ebp+KTRAP_FRAME_EDX]
    mov [ebx+KTHREAD_TRAP_FRAME], edx

    /* Set up stack frame */
    mov ebp, esp

    /* Get the Trap Frame in EBX */
    mov ebx, [ebp+0]

    /* Get the exception list and restore */
    mov eax, [ebx+KTRAP_FRAME_EXCEPTION_LIST]
    mov [fs:KPCR_EXCEPTION_LIST], eax

    /* Get the parameters */
    mov edx, [ebp+16] /* Search frames */
    mov ecx, [ebp+12] /* Context */
    mov eax, [ebp+8]  /* Exception Record */

    /* Raise the exception */
    push edx
    push ebx
    push 0
    push ecx
    push eax
    call _KiRaiseException@20

    /* Restore trap frame in EBP */
    pop ebp
    mov esp, ebp

    /* Check the result */
    or eax, eax
    jz _KiServiceExit2

    /* Restore debug registers too */
    jmp _KiServiceExit
.endfunc

.func NtContinue@8
_NtContinue@8:

    /* NOTE: We -must- be called by Zw* to have the right frame! */
    /* Push the stack frame */
    push ebp

    /* Get the current thread and restore its trap frame */
    mov ebx, [fs:KPCR_CURRENT_THREAD]
    mov edx, [ebp+KTRAP_FRAME_EDX]
    mov [ebx+KTHREAD_TRAP_FRAME], edx

    /* Set up stack frame */
    mov ebp, esp

    /* Save the parameters */
    mov eax, [ebp+0]
    mov ecx, [ebp+8]

    /* Call KiContinue */
    push eax
    push 0
    push ecx
    call _KiContinue@12

    /* Check if we failed (bad context record) */
    or eax, eax
    jnz Error

    /* Check if test alert was requested */
    cmp dword ptr [ebp+12], 0
    je DontTest

    /* Test alert for the thread */
    mov al, [ebx+KTHREAD_PREVIOUS_MODE]
    push eax
    call _KeTestAlertThread@4

DontTest:
    /* Return to previous context */
    pop ebp
    mov esp, ebp
    jmp _KiServiceExit2

Error:
    pop ebp
    mov esp, ebp
    jmp _KiServiceExit
.endfunc

_KiTrap0:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(0)

    /* Call the C exception handler */
    push 0
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap1:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(1)

    /* Call the C exception handler */
    push 1
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap2:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(2)

    /* Call the C exception handler */
    push 2
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap3:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(3)

    /* Call the C exception handler */
    push 3
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap4:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(4)

    /* Call the C exception handler */
    push 4
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap5:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(5)

    /* Call the C exception handler */
    push 5
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap6:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(6)

    /* Call the C exception handler */
    push 6
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap7:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(7)

    /* Call the C exception handler */
    push 7
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

.globl _KiTrap8
_KiTrap8:
    call _KiDoubleFaultHandler
    iret

_KiTrap9:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(9)

    /* Call the C exception handler */
    push 9
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap10:
    /* Enter trap */
    TRAP_PROLOG(10)

    /* Call the C exception handler */
    push 10
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap11:
    /* Enter trap */
    TRAP_PROLOG(11)

    /* Call the C exception handler */
    push 11
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap12:
    /* Enter trap */
    TRAP_PROLOG(12)

    /* Call the C exception handler */
    push 12
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap13:
    /* Enter trap */
    TRAP_PROLOG(13)

    /* Call the C exception handler */
    push 13
    push ebp
    call _KiTrapHandler
    add esp, 8
    
    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap14:
    /* Enter trap */
    TRAP_PROLOG(14)

    /* Call the C exception handler */
    push 14
    push ebp
    call _KiPageFaultHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap15:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(15)

    /* Call the C exception handler */
    push 15
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap16:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(16)

    /* Call the C exception handler */
    push 16
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap17:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(17)

    /* Call the C exception handler */
    push 17
    push ebp
    call _KiTrapHandler
    add esp, 8

    /* Check for v86 recovery */
    cmp eax, 1

    /* Return to caller */
    jne _Kei386EoiHelper@0
    jmp _KiV86Complete

_KiTrap0F:
    /* Push error code */
    push 0

    /* Enter trap */
    TRAP_PROLOG(15)
    sti

    /* Raise a fatal exception */
    mov eax, 15
    jmp _KiSystemFatalException

.func KiSystemFatalException
_KiSystemFatalException:

    /* Push the trap frame */
    push ebp

    /* Push empty parameters */
    push 0
    push 0
    push 0

    /* Push trap number and bugcheck code */
    push eax
    push UNEXPECTED_KERNEL_MODE_TRAP
    call _KeBugCheckWithTf@24
    ret
.endfunc


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?