--- /dev/null
+;------------------------------------------------------------------------------\r
+;\r
+; Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
+; This program and the accompanying materials\r
+; are licensed and made available under the terms and conditions of the BSD License\r
+; which accompanies this distribution. The full text of the license may be found at\r
+; http://opensource.org/licenses/bsd-license.php.\r
+;\r
+; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+;\r
+; Module Name:\r
+;\r
+; AsmFuncs.nasm\r
+;\r
+; Abstract:\r
+;\r
+; Debug interrupt handle functions.\r
+;\r
+;------------------------------------------------------------------------------\r
+\r
+#include "DebugException.h"\r
+\r
+SECTION .data\r
+\r
+extern ASM_PFX(InterruptProcess)\r
+global ASM_PFX(Exception0Handle)\r
+global ASM_PFX(TimerInterruptHandle)\r
+global ASM_PFX(ExceptionStubHeaderSize)\r
+\r
+%macro AGENT_HANDLER_SIGNATURE 0\r
+ db 0x41, 0x47, 0x54, 0x48 ; SIGNATURE_32('A','G','T','H')\r
+%endmacro\r
+\r
+ASM_PFX(ExceptionStubHeaderSize): dd Exception1Handle - ASM_PFX(Exception0Handle) ;\r
+CommonEntryAddr: dq CommonEntry ;\r
+\r
+DEFAULT REL\r
+SECTION .text\r
+\r
+AGENT_HANDLER_SIGNATURE\r
+ASM_PFX(Exception0Handle):\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 0\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception1Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 1\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception2Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 2\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception3Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 3\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception4Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 4\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception5Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 5\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception6Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 6\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception7Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 7\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception8Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 8\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception9Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 9\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception10Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 10\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception11Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 11\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception12Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 12\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception13Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 13\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception14Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 14\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception15Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 15\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception16Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 16\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception17Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 17\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception18Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 18\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+Exception19Handle:\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 19\r
+ jmp qword [CommonEntryAddr]\r
+AGENT_HANDLER_SIGNATURE\r
+ASM_PFX(TimerInterruptHandle):\r
+ cli\r
+ push rcx\r
+ mov rcx, dword 32\r
+ jmp qword [CommonEntryAddr]\r
+\r
+CommonEntry:\r
+ ; We need to determine if any extra data was pushed by the exception\r
+ cmp rcx, DEBUG_EXCEPT_DOUBLE_FAULT\r
+ je NoExtrPush\r
+ cmp rcx, DEBUG_EXCEPT_INVALID_TSS\r
+ je NoExtrPush\r
+ cmp rcx, DEBUG_EXCEPT_SEG_NOT_PRESENT\r
+ je NoExtrPush\r
+ cmp rcx, DEBUG_EXCEPT_STACK_FAULT\r
+ je NoExtrPush\r
+ cmp rcx, DEBUG_EXCEPT_GP_FAULT\r
+ je NoExtrPush\r
+ cmp rcx, DEBUG_EXCEPT_PAGE_FAULT\r
+ je NoExtrPush\r
+ cmp rcx, DEBUG_EXCEPT_ALIGNMENT_CHECK\r
+ je NoExtrPush\r
+\r
+ push qword [rsp]\r
+ mov qword [rsp + 8], 0\r
+\r
+NoExtrPush:\r
+ push rbp\r
+ mov rbp, rsp\r
+\r
+ ; store UINT64 r8, r9, r10, r11, r12, r13, r14, r15;\r
+ push r15\r
+ push r14\r
+ push r13\r
+ push r12\r
+ push r11\r
+ push r10\r
+ push r9\r
+ push r8\r
+\r
+ mov r8, cr8\r
+ push r8\r
+\r
+ ; store UINT64 Rdi, Rsi, Rbp, Rsp, Rdx, Rcx, Rbx, Rax;\r
+ push rax\r
+ push rbx\r
+ push qword [rbp + 8] ; original rcx\r
+ push rdx\r
+ push qword [rbp + 6 * 8] ; original rsp\r
+ push qword [rbp] ; original rbp\r
+ push rsi\r
+ push rdi\r
+\r
+ ;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;\r
+ ;; insure FXSAVE/FXRSTOR is enabled in CR4...\r
+ ;; ... while we're at it, make sure DE is also enabled...\r
+ mov rax, cr4\r
+ or rax, 0x208\r
+ mov cr4, rax\r
+ push rax\r
+ mov rax, cr3\r
+ push rax\r
+ mov rax, cr2\r
+ push rax\r
+ push 0\r
+ mov rax, cr0\r
+ push rax\r
+\r
+ xor rax, rax\r
+ mov rax, Ss\r
+ push rax\r
+ mov rax, Cs\r
+ push rax\r
+ mov rax, Ds\r
+ push rax\r
+ mov rax, Es\r
+ push rax\r
+ mov rax, Fs\r
+ push rax\r
+ mov rax, Gs\r
+ push rax\r
+\r
+ ;; EIP\r
+ mov rax, [rbp + 8 * 3] ; EIP\r
+ push rax\r
+\r
+ ;; UINT64 Gdtr[2], Idtr[2];\r
+ sub rsp, 16\r
+ sidt [rsp]\r
+ sub rsp, 16\r
+ sgdt [rsp]\r
+\r
+ ;; UINT64 Ldtr, Tr;\r
+ xor rax, rax\r
+ str ax\r
+ push rax\r
+ sldt ax\r
+ push rax\r
+\r
+ ;; EFlags\r
+ mov rax, [rbp + 8 * 5]\r
+ push rax\r
+\r
+ ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
+ mov rax, dr7\r
+ push rax\r
+\r
+ ;; clear Dr7 while executing debugger itself\r
+ xor rax, rax\r
+ mov dr7, rax\r
+\r
+ ;; Dr6\r
+ mov rax, dr6\r
+ push rax\r
+\r
+ ;; insure all status bits in dr6 are clear...\r
+ xor rax, rax\r
+ mov dr6, rax\r
+\r
+ mov rax, dr3\r
+ push rax\r
+ mov rax, dr2\r
+ push rax\r
+ mov rax, dr1\r
+ push rax\r
+ mov rax, dr0\r
+ push rax\r
+\r
+ ;; Clear Direction Flag\r
+ cld\r
+\r
+ sub rsp, 512\r
+ mov rdi, rsp\r
+ ;; Clear the buffer\r
+ xor rax, rax\r
+ push rcx\r
+ mov rcx, dword 64 ;= 512 / 8\r
+ rep stosq\r
+ pop rcx\r
+ mov rdi, rsp\r
+ db 0xf, 0xae, 00000111y ;fxsave [rdi]\r
+\r
+ ;; save the exception data\r
+ push qword [rbp + 16]\r
+\r
+ ; call the C interrupt process function\r
+ mov rdx, rsp ; Structure\r
+ mov r15, rcx ; save vector in r15\r
+\r
+ ;\r
+ ; Per X64 calling convention, allocate maximum parameter stack space\r
+ ; and make sure RSP is 16-byte aligned\r
+ ;\r
+ sub rsp, 32 + 8\r
+ call ASM_PFX(InterruptProcess)\r
+ add rsp, 32 + 8\r
+\r
+ ;; skip the exception data\r
+ add rsp, 8\r
+\r
+ mov rsi, rsp\r
+ db 0xf, 0xae, 00001110y ; fxrstor [rsi]\r
+ add rsp, 512\r
+\r
+ ;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
+ pop rax\r
+ mov dr0, rax\r
+ pop rax\r
+ mov dr1, rax\r
+ pop rax\r
+ mov dr2, rax\r
+ pop rax\r
+ mov dr3, rax\r
+ ;; skip restore of dr6. We cleared dr6 during the context save.\r
+ add rsp, 8\r
+ pop rax\r
+ mov dr7, rax\r
+\r
+ ;; set EFlags\r
+ pop qword [rbp + 8 * 5]\r
+\r
+ ;; UINT64 Ldtr, Tr;\r
+ ;; UINT64 Gdtr[2], Idtr[2];\r
+ ;; Best not let anyone mess with these particular registers...\r
+ add rsp, 24 * 2\r
+\r
+ ;; UINT64 Eip;\r
+ pop qword [rbp + 8 * 3] ; set EIP in stack\r
+\r
+ ;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;\r
+ ;; NOTE - modified segment registers could hang the debugger... We\r
+ ;; could attempt to insulate ourselves against this possibility,\r
+ ;; but that poses risks as well.\r
+ ;;\r
+ pop rax\r
+ pop rax\r
+ pop rax\r
+ mov es, rax\r
+ pop rax\r
+ mov ds, rax\r
+ pop qword [rbp + 8 * 4] ; Set CS in stack\r
+ pop rax\r
+ mov ss, rax\r
+\r
+ ;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4;\r
+ pop rax\r
+ mov cr0, rax\r
+ add rsp, 8 ; skip for Cr1\r
+ pop rax\r
+ mov cr2, rax\r
+ pop rax\r
+ mov cr3, rax\r
+ pop rax\r
+ mov cr4, rax\r
+\r
+ ;; restore general register\r
+ pop rdi\r
+ pop rsi\r
+ add rsp, 8 ; skip rbp\r
+ add rsp, 8 ; skip rsp\r
+ pop rdx\r
+ pop rcx\r
+ pop rbx\r
+ pop rax\r
+\r
+ pop r8\r
+ mov cr8, r8\r
+\r
+ ; store UINT64 r8, r9, r10, r11, r12, r13, r14, r15;\r
+ pop r8\r
+ pop r9\r
+ pop r10\r
+ pop r11\r
+ pop r12\r
+ pop r13\r
+ pop r14\r
+ pop r15\r
+\r
+ mov rsp, rbp\r
+ pop rbp\r
+ add rsp, 16 ; skip rcx and error code\r
+\r
+ iretq\r
+\r