]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionHandlerAsm.asm
1. Separated DxeSmmCpuExceptionHandlerLib.inf into 2 instance DxeCpuExceptionHandlerL...
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / Ia32 / ExceptionHandlerAsm.asm
index 869f18dfde054108b70b782cc88e7bb1731f7a92..74d4e89047108c654027e213138ab7fcbdddda02 100644 (file)
@@ -1,5 +1,5 @@
 ;------------------------------------------------------------------------------ ;\r
-; Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+; Copyright (c) 2012 - 2013, 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
@@ -30,133 +30,77 @@ CommonExceptionHandler             PROTO   C
 \r
 .data\r
 \r
-CommonEntryAddr           DD    CommonInterruptEntry\r
-\r
-EXTRN mErrorCodeFlag:DWORD             ; Error code flags for exceptions\r
+EXTRN mErrorCodeFlag:DWORD            ; Error code flags for exceptions\r
+EXTRN mDoFarReturnFlag:DWORD          ; Do far return flag\r
 \r
 .code\r
 \r
+ALIGN   8\r
+\r
 ;\r
 ; exception handler stub table\r
 ;\r
-Exception0Handle:\r
-    push    0\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception1Handle:\r
-    push    1\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception2Handle:\r
-    push    2\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception3Handle:\r
-    push    3\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception4Handle:\r
-    push    4\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception5Handle:\r
-    push    5\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception6Handle:\r
-    push    6\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception7Handle:\r
-    push    7\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception8Handle:\r
-    push    8\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception9Handle:\r
-    push    9\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception10Handle:\r
-    push    10\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception11Handle:\r
-    push    11\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception12Handle:\r
-    push    12\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception13Handle:\r
-    push    13\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception14Handle:\r
-    push    14\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception15Handle:\r
-    push    15\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception16Handle:\r
-    push    16\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception17Handle:\r
-    push    17\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception18Handle:\r
-    push    18\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception19Handle:\r
-    push    19\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception20Handle:\r
-    push    20\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception21Handle:\r
-    push    21\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception22Handle:\r
-    push    22\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception23Handle:\r
-    push    23\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception24Handle:\r
-    push    24\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception25Handle:\r
-    push    25\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception26Handle:\r
-    push    26\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception27Handle:\r
-    push    27\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception28Handle:\r
-    push    28\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception29Handle:\r
-    push    29\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception30Handle:\r
-    push    30\r
-    jmp     dword ptr [CommonEntryAddr]\r
-Exception31Handle:\r
-    push    31\r
-    jmp     dword ptr [CommonEntryAddr]\r
+AsmIdtVectorBegin:\r
+REPEAT  32\r
+    db      6ah        ; push  #VectorNum\r
+    db      ($ - AsmIdtVectorBegin) / ((AsmIdtVectorEnd - AsmIdtVectorBegin) / 32) ; VectorNum\r
+    push    eax\r
+    mov     eax, CommonInterruptEntry\r
+    jmp     eax\r
+ENDM\r
+AsmIdtVectorEnd:\r
+\r
+HookAfterStubBegin:\r
+    db      6ah        ; push\r
+VectorNum:\r
+    db      0          ; 0 will be fixed \r
+    push    eax\r
+    mov     eax, HookAfterStubHeaderEnd\r
+    jmp     eax\r
+HookAfterStubHeaderEnd:\r
+    pop     eax\r
+    sub     esp, 8     ; reserve room for filling exception data later\r
+    push    [esp + 8]\r
+    xchg    ecx, [esp] ; get vector number\r
+    bt      mErrorCodeFlag, ecx\r
+    jnc     @F\r
+    push    [esp]      ; addition push if exception data needed\r
+@@:\r
+    xchg    ecx, [esp] ; restore ecx\r
+    push    eax\r
 \r
 ;----------------------------------------------------------------------------;\r
 ; CommonInterruptEntry                                                               ;\r
 ;----------------------------------------------------------------------------;\r
 ; The follow algorithm is used for the common interrupt routine.\r
 ; Entry from each interrupt with a push eax and eax=interrupt number\r
-\r
+; Stack:\r
+; +---------------------+\r
+; +    EFlags           +\r
+; +---------------------+\r
+; +    CS               +\r
+; +---------------------+\r
+; +    EIP              +\r
+; +---------------------+\r
+; +    Error Code       +\r
+; +---------------------+\r
+; +    Vector Number    +\r
+; +---------------------+\r
+; +    EBP              +\r
+; +---------------------+ <-- EBP\r
 CommonInterruptEntry PROC PUBLIC\r
     cli\r
+    pop    eax\r
     ;\r
     ; All interrupt handlers are invoked through interrupt gates, so\r
     ; IF flag automatically cleared at the entry point\r
     ;\r
 \r
     ;\r
-    ; Calculate vector number\r
-    ;\r
-    ; Get the return address of call, actually, it is the\r
-    ; address of vector number.\r
+    ; Get vector number from top of stack\r
     ;\r
     xchg    ecx, [esp]\r
-    and     ecx, 0FFFFh\r
+    and     ecx, 0FFh       ; Vector number should be less than 256\r
     cmp     ecx, 32         ; Intel reserved vector for exceptions?\r
     jae     NoErrorCode\r
     bt      mErrorCodeFlag, ecx\r
@@ -246,6 +190,10 @@ ErrorCodeAndVectorOnStack:
     and     esp, 0fffffff0h\r
     sub     esp, 12\r
 \r
+    sub     esp, 8\r
+    push    0            ; clear EXCEPTION_HANDLER_CONTEXT.OldIdtHandler\r
+    push    0            ; clear EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag\r
+       \r
 ;; UINT32  Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
     push    eax\r
     push    ecx\r
@@ -411,19 +359,42 @@ ErrorCodeAndVectorOnStack:
     pop     ecx\r
     pop     eax\r
 \r
+    pop     dword ptr [ebp - 8]\r
+    pop     dword ptr [ebp - 4]\r
     mov     esp, ebp\r
     pop     ebp\r
     add     esp, 8\r
+    cmp     dword ptr [esp - 16], 0   ; check EXCEPTION_HANDLER_CONTEXT.OldIdtHandler\r
+    jz      DoReturn\r
+    cmp     dword ptr [esp - 20], 1   ; check EXCEPTION_HANDLER_CONTEXT.ExceptionDataFlag\r
+    jz      ErrorCode\r
+    jmp     dword ptr [esp - 16]\r
+ErrorCode:\r
+    sub     esp, 4\r
+    jmp     dword ptr [esp - 12]\r
+\r
+DoReturn:    \r
+    cmp     mDoFarReturnFlag, 0   ; Check if need to do far return instead of IRET\r
+    jz      DoIret\r
+    push    [esp + 8]    ; save EFLAGS\r
+    add     esp, 16\r
+    push    [esp - 8]    ; save CS in new location\r
+    push    [esp - 8]    ; save EIP in new location\r
+    push    [esp - 8]    ; save EFLAGS in new location\r
+    popfd                ; restore EFLAGS\r
+    retf                 ; far return\r
+\r
+DoIret:\r
     iretd\r
 \r
 CommonInterruptEntry ENDP\r
 \r
 ;---------------------------------------;\r
-; _GetTemplateAddressMap                  ;\r
+; _AsmGetTemplateAddressMap                  ;\r
 ;----------------------------------------------------------------------------;\r
 ; \r
 ; Protocol prototype\r
-;   GetTemplateAddressMap (\r
+;   AsmGetTemplateAddressMap (\r
 ;     EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap\r
 ;   );\r
 ;           \r
@@ -447,18 +418,28 @@ CommonInterruptEntry ENDP
 ;           \r
 ; Destroys: Nothing\r
 ;-----------------------------------------------------------------------------;\r
-GetTemplateAddressMap  proc near public\r
+AsmGetTemplateAddressMap  proc near public\r
     push    ebp                 ; C prolog\r
     mov     ebp, esp\r
     pushad\r
 \r
-    mov ebx, dword ptr [ebp+08h]\r
-    mov dword ptr [ebx],    Exception0Handle\r
-    mov dword ptr [ebx+4h], Exception1Handle - Exception0Handle\r
+    mov ebx, dword ptr [ebp + 08h]\r
+    mov dword ptr [ebx],      AsmIdtVectorBegin\r
+    mov dword ptr [ebx + 4h], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32\r
+    mov dword ptr [ebx + 8h], HookAfterStubBegin\r
   \r
     popad\r
     pop     ebp\r
     ret\r
-GetTemplateAddressMap  ENDP\r
-\r
+AsmGetTemplateAddressMap  ENDP\r
+\r
+;-------------------------------------------------------------------------------------\r
+;  AsmVectorNumFixup (*VectorBase, VectorNum);\r
+;-------------------------------------------------------------------------------------\r
+AsmVectorNumFixup   proc near public\r
+    mov     eax, dword ptr [esp + 8]\r
+    mov     ecx, [esp + 4]\r
+    mov     [ecx + (VectorNum - HookAfterStubBegin)], al\r
+    ret\r
+AsmVectorNumFixup   ENDP\r
 END\r