Skip to content

Commit

Permalink
Write exit label for runctions and interrupt handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbetros committed Aug 4, 2019
1 parent c4773d8 commit 5d7adf5
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 169 deletions.
133 changes: 0 additions & 133 deletions playground/AsmBreak.asm
Original file line number Original file line Diff line number Diff line change
@@ -1,133 +0,0 @@
; namespace DebugStub

; Location where INT3 has been injected.
; 0 if no INT3 is active.
; var AsmBreakEIP
DebugStub_AsmBreakEIP dd 0

; Old byte before INT3 was injected.
; Only 1 byte is used.
; var AsmOrigByte
DebugStub_AsmOrigByte dd 0

; function DoAsmBreak {
DebugStub_DoAsmBreak:
; Since our Int3 is temp, we need to adjust return EIP to return to it, not after it.
; ESI = .CallerESP
Mov ESI, DWORD [DebugStub_CallerESP]
; EAX = .AsmBreakEIP
Mov EAX, DWORD [DebugStub_AsmBreakEIP]
; [ESI-12] = EAX
Mov DWORD [ESI - 12], EAX

; ClearAsmBreak()
Call DebugStub_ClearAsmBreak
; Break()
Call DebugStub_Break
; }

; function SetAsmBreak {
DebugStub_SetAsmBreak:
; ClearAsmBreak()
Call DebugStub_ClearAsmBreak

; ComReadEAX()
Call DebugStub_ComReadEAX
; Save EIP of the break
; .AsmBreakEIP = EAX
Mov DWORD [DebugStub_AsmBreakEIP], EAX
; EDI = EAX
Mov EDI, EAX

; Save the old byte
; AL = [EDI]
Mov AL, BYTE [EDI]
; .AsmOrigByte = AL
Mov BYTE [DebugStub_AsmOrigByte], AL

; Inject INT3
; Do in 2 steps to force a byte move to RAM (till X# can do byte in one step)
; AL = $CC
Mov AL, 0xCC
; [EDI] = AL
Mov BYTE [EDI], AL
; }

; function ClearAsmBreak {
DebugStub_ClearAsmBreak:
; EDI = .AsmBreakEIP
Mov EDI, DWORD [DebugStub_AsmBreakEIP]
; If 0, we don't need to clear an older one.
; if EDI = 0 return
; Clear old break point and set back to original opcode / partial opcode
; AL = .AsmOrigByte
Mov AL, BYTE [DebugStub_AsmOrigByte]
; [EDI] = AL
Mov BYTE [EDI], AL

; .AsmBreakEIP = 0
Mov DWORD [DebugStub_AsmBreakEIP], 0x0
; }

; function SetINT1_TrapFLAG {
DebugStub_SetINT1_TrapFLAG:
; Push EAX to make sure whatever we do below doesn't affect code afterwards
; +EBP
Push EBP
; +EAX
Push EAX

; Set base pointer to the caller ESP
; EBP = .CallerESP
Mov EBP, DWORD [DebugStub_CallerESP]
; Set the Trap Flag (http://en.wikipedia.org/wiki/Trap_flag)
; For EFLAGS we want - the interrupt frame = ESP + 12
; - The interrupt frame - 8 for correct byte = ESP + 12 - 8 = ESP + 4
; - Therefore, ESP - 4 to get to the correct position
; EBP -= 4
Sub EBP, 0x4
; EAX = [EBP]
Mov EAX, DWORD [EBP]
; EAX | $0100
; [EBP] = EAX
Mov DWORD [EBP], EAX

; Restore the base pointer
; Pop EAX - see +EAX at start of method
; -EAX
Pop EAX
; -EBP
Pop EBP
; }

; function ResetINT1_TrapFLAG {
DebugStub_ResetINT1_TrapFLAG:
; Push EAX to make sure whatever we do below doesn't affect code afterwards
; +EBP
Push EBP
; +EAX
Push EAX

; Set base pointer to the caller ESP
; EBP = .CallerESP
Mov EBP, DWORD [DebugStub_CallerESP]
; Clear the Trap Flag (http://en.wikipedia.org/wiki/Trap_flag)
; See comment in SetINT1_TrapFlag
; EBP -= 4
Sub EBP, 0x4
; EAX = [EBP]
Mov EAX, DWORD [EBP]
; EAX & $FEFF
; [EBP] = EAX
Mov DWORD [EBP], EAX
; Pop EAX - see +EAX at start of method
; -EAX
Pop EAX
; -EBP
Pop EBP
; }
6 changes: 6 additions & 0 deletions playground/CmdMisc.asm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ DebugStub_Ping:
; ComWriteAL() ; ComWriteAL()
Call DebugStub_ComWriteAL Call DebugStub_ComWriteAL
; } ; }
DebugStub_Ping_Exit:
Ret


; function TraceOn { ; function TraceOn {
DebugStub_TraceOn: DebugStub_TraceOn:
; Tracing.On ; Tracing.On
; .TraceMode = 1 ; .TraceMode = 1
Mov DWORD [DebugStub_TraceMode], 0x1 Mov DWORD [DebugStub_TraceMode], 0x1
; } ; }
DebugStub_TraceOn_Exit:
Ret


; function TraceOff { ; function TraceOff {
DebugStub_TraceOff: DebugStub_TraceOff:
; Tracing.Off ; Tracing.Off
; .TraceMode = 0 ; .TraceMode = 0
Mov DWORD [DebugStub_TraceMode], 0x0 Mov DWORD [DebugStub_TraceMode], 0x0
; } ; }
DebugStub_TraceOff_Exit:
Ret
4 changes: 4 additions & 0 deletions playground/CmdProcess.asm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ DebugStub_AckCommand:
; ComWriteAL() ; ComWriteAL()
Call DebugStub_ComWriteAL Call DebugStub_ComWriteAL
; } ; }
DebugStub_AckCommand_Exit:
Ret


; function ProcessCommandBatch { ; function ProcessCommandBatch {
DebugStub_ProcessCommandBatch: DebugStub_ProcessCommandBatch:
Expand All @@ -188,3 +190,5 @@ DebugStub_ProcessCommandBatch_Begin:
; AckCommand() ; AckCommand()
Call DebugStub_AckCommand Call DebugStub_AckCommand
; } ; }
DebugStub_ProcessCommandBatch_Exit:
Ret
36 changes: 36 additions & 0 deletions playground/CmdSend.asm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ DebugStub_SendRegisters:
; ComWrite32() ; ComWrite32()
Call DebugStub_ComWrite32 Call DebugStub_ComWrite32
; } ; }
DebugStub_SendRegisters_Exit:
Ret


; function SendFrame { ; function SendFrame {
DebugStub_SendFrame: DebugStub_SendFrame:
Expand All @@ -48,6 +50,8 @@ DebugStub_SendFrame:
; ComWriteX() ; ComWriteX()
Call DebugStub_ComWriteX Call DebugStub_ComWriteX
; } ; }
DebugStub_SendFrame_Exit:
Ret


; AL contains channel ; AL contains channel
; BL contains command ; BL contains command
Expand Down Expand Up @@ -91,6 +95,8 @@ DebugStub_SendCommandOnChannel:
; } ; }
DebugStub_SendCommandOnChannel_Block1_End: DebugStub_SendCommandOnChannel_Block1_End:
; } ; }
DebugStub_SendCommandOnChannel_Exit:
Ret


; function SendStack { ; function SendStack {
DebugStub_SendStack: DebugStub_SendStack:
Expand Down Expand Up @@ -119,6 +125,8 @@ DebugStub_SendStack:
; } ; }
DebugStub_SendStack_Block1_End: DebugStub_SendStack_Block1_End:
; } ; }
DebugStub_SendStack_Exit:
Ret


; sends a stack value ; sends a stack value
; Serial Params: ; Serial Params:
Expand Down Expand Up @@ -228,6 +236,8 @@ DebugStub_SendTrace:
; ComWrite32() ; ComWrite32()
Call DebugStub_ComWrite32 Call DebugStub_ComWrite32
; } ; }
DebugStub_SendTrace_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand Down Expand Up @@ -286,6 +296,8 @@ DebugStub_SendText_Finalize:
; -EBP ; -EBP
Pop EBP Pop EBP
; } ; }
DebugStub_SendText_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand Down Expand Up @@ -315,6 +327,8 @@ Mov EBP, ESP
; -EBP ; -EBP
Pop EBP Pop EBP
; } ; }
DebugStub_SendSimpleNumber_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand Down Expand Up @@ -346,6 +360,8 @@ Mov EBP, ESP
; -EBP ; -EBP
Pop EBP Pop EBP
; } ; }
DebugStub_SendKernelPanic_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand Down Expand Up @@ -380,6 +396,8 @@ DebugStub_SendSimpleLongNumber:
; -EBP ; -EBP
Pop EBP Pop EBP
; } ; }
DebugStub_SendSimpleLongNumber_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand Down Expand Up @@ -410,6 +428,8 @@ DebugStub_SendComplexNumber:
; -EBP ; -EBP
Pop EBP Pop EBP
; } ; }
DebugStub_SendComplexNumber_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand Down Expand Up @@ -444,6 +464,8 @@ DebugStub_SendComplexLongNumber:
; -EBP ; -EBP
Pop EBP Pop EBP
; } ; }
DebugStub_SendComplexLongNumber_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand All @@ -462,6 +484,8 @@ DebugStub_SendPtr:
; ComWrite32() ; ComWrite32()
Call DebugStub_ComWrite32 Call DebugStub_ComWrite32
; } ; }
DebugStub_SendPtr_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand All @@ -480,6 +504,8 @@ DebugStub_SendStackCorruptionOccurred:
; ComWrite32() ; ComWrite32()
Call DebugStub_ComWrite32 Call DebugStub_ComWrite32
; } ; }
DebugStub_SendStackCorruptionOccurred_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand All @@ -498,6 +524,8 @@ DebugStub_SendStackOverflowOccurred:
; ComWrite32() ; ComWrite32()
Call DebugStub_ComWrite32 Call DebugStub_ComWrite32
; } ; }
DebugStub_SendStackOverflowOccurred_Exit:
Ret


; Input: None ; Input: None
; Output: None ; Output: None
Expand All @@ -518,6 +546,8 @@ DebugStub_SendInterruptOccurred:
; ComWriteEAX() ; ComWriteEAX()
Call DebugStub_ComWriteEAX Call DebugStub_ComWriteEAX
; } ; }
DebugStub_SendInterruptOccurred_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand All @@ -536,6 +566,8 @@ DebugStub_SendNullReferenceOccurred:
; ComWrite32() ; ComWrite32()
Call DebugStub_ComWrite32 Call DebugStub_ComWrite32
; } ; }
DebugStub_SendNullReferenceOccurred_Exit:
Ret


; Input: Stack ; Input: Stack
; Output: None ; Output: None
Expand Down Expand Up @@ -575,6 +607,8 @@ DebugStub_SendMessageBox_WriteChar:
; goto WriteChar ; goto WriteChar
Jmp DebugStub_SendMessageBox_WriteChar Jmp DebugStub_SendMessageBox_WriteChar
; } ; }
DebugStub_SendMessageBox_Exit:
Ret


; function SendCoreDump { ; function SendCoreDump {
DebugStub_SendCoreDump: DebugStub_SendCoreDump:
Expand Down Expand Up @@ -637,3 +671,5 @@ DebugStub_SendCoreDump:
; } ; }
DebugStub_SendCoreDump_Block2_End: DebugStub_SendCoreDump_Block2_End:
; } ; }
DebugStub_SendCoreDump_Exit:
Ret
4 changes: 4 additions & 0 deletions playground/DebugStub.asm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ DebugStub_Executing_CheckForCmd:
; } ; }
DebugStub_Executing_Block11_End: DebugStub_Executing_Block11_End:
; } ; }
DebugStub_Executing_Exit:
Ret


; function Break { ; function Break {
DebugStub_Break: DebugStub_Break:
Expand Down Expand Up @@ -448,4 +450,6 @@ DebugStub_Break_Done:
; .DebugStatus = #Status_Run ; .DebugStatus = #Status_Run
Mov [DebugStub_DebugStatus], DebugStub_Const_Status_Run Mov [DebugStub_DebugStatus], DebugStub_Const_Status_Run
; } ; }
DebugStub_Break_Exit:
Ret


8 changes: 8 additions & 0 deletions playground/Init.asm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ DebugStub_Init:
; Cls() ; Cls()
Call DebugStub_Cls Call DebugStub_Cls
; } ; }
DebugStub_Init_Exit:
Ret


; function WaitForSignature { ; function WaitForSignature {
DebugStub_WaitForSignature: DebugStub_WaitForSignature:
Expand All @@ -34,6 +36,8 @@ DebugStub_WaitForSignature:
; } ; }
DebugStub_WaitForSignature_Block1_End: DebugStub_WaitForSignature_Block1_End:
; } ; }
DebugStub_WaitForSignature_Exit:
Ret


; QEMU (and possibly others) send some garbage across the serial line first. ; QEMU (and possibly others) send some garbage across the serial line first.
; Actually they send the garbage inbound, but garbage could be inbound as well so we ; Actually they send the garbage inbound, but garbage could be inbound as well so we
Expand Down Expand Up @@ -85,11 +89,15 @@ DebugStub_WaitForDbgHandshake:
; Hook_OnHandshakeCompleted() ; Hook_OnHandshakeCompleted()
Call DebugStub_Hook_OnHandshakeCompleted Call DebugStub_Hook_OnHandshakeCompleted
; } ; }
DebugStub_WaitForDbgHandshake_Exit:
Ret


; //! %ifndef Exclude_Dummy_Hooks ; //! %ifndef Exclude_Dummy_Hooks
%ifndef Exclude_Dummy_Hooks %ifndef Exclude_Dummy_Hooks
; function Hook_OnHandshakeCompleted { ; function Hook_OnHandshakeCompleted {
DebugStub_Hook_OnHandshakeCompleted: DebugStub_Hook_OnHandshakeCompleted:
; } ; }
DebugStub_Hook_OnHandshakeCompleted_Exit:
Ret
; //! %endif ; //! %endif
%endif %endif
4 changes: 4 additions & 0 deletions playground/Screen.asm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ DebugStub_Cls:
; } ; }
DebugStub_Cls_Block1_End: DebugStub_Cls_Block1_End:
; } ; }
DebugStub_Cls_Exit:
Ret


; function DisplayWaitMsg { ; function DisplayWaitMsg {
DebugStub_DisplayWaitMsg: DebugStub_DisplayWaitMsg:
Expand All @@ -55,6 +57,8 @@ DebugStub_DisplayWaitMsg:
; } ; }
DebugStub_DisplayWaitMsg_Block1_End: DebugStub_DisplayWaitMsg_Block1_End:
; } ; }
DebugStub_DisplayWaitMsg_Exit:
Ret


; //! %endif ; //! %endif
%endif %endif
Loading

0 comments on commit 5d7adf5

Please sign in to comment.