summaryrefslogtreecommitdiffstats
path: root/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/Ia32/ArchExceptionHandlerTestAsm.nasm
blob: 48031a510981215e3d1d6249077cf4fa8b306d3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
;------------------------------------------------------------------------------
;
; Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
; SPDX-License-Identifier: BSD-2-Clause-Patent
;
; Module Name:
;
;   ArchExceptionHandlerTestAsm.nasm
;
; Abstract:
;
;   ia32 CPU Exception Handler Lib Unit test
;
;------------------------------------------------------------------------------

    SECTION .text

struc GENERAL_REGISTER_IA32
  .Edi:    resd    1
  .Esi:    resd    1
  .Ebx:    resd    1
  .Edx:    resd    1
  .Ecx:    resd    1
  .Eax:    resd    1

endstruc

extern ASM_PFX(mExpectedContextInHandler)
extern ASM_PFX(mActualContextAfterException)
extern ASM_PFX(mFaultInstructionLength)

;------------------------------------------------------------------------------
; VOID
; EFIAPI
; TriggerGPException (
;  UINTN  Cr4ReservedBit
;  );
;------------------------------------------------------------------------------
global ASM_PFX(TriggerGPException)
ASM_PFX(TriggerGPException):
    ;
    ; Set reserved bit 15 of cr4 to 1
    ;
    lea  ecx, [ASM_PFX(mFaultInstructionLength)]
    mov  dword[ecx], TriggerGPExceptionAfter - TriggerGPExceptionBefore
    mov  ecx, dword [esp + 0x4]
TriggerGPExceptionBefore:
    mov  cr4, ecx
TriggerGPExceptionAfter:
    ret

;------------------------------------------------------------------------------
; VOID
; EFIAPI
; TriggerPFException (
;  UINTN  PfAddress
;  );
;------------------------------------------------------------------------------
global ASM_PFX(TriggerPFException)
ASM_PFX(TriggerPFException):
    lea  ecx, [ASM_PFX(mFaultInstructionLength)]
    mov  dword[ecx], TriggerPFExceptionAfter - TriggerPFExceptionBefore
    mov  ecx, dword [esp + 0x4]
TriggerPFExceptionBefore:
    mov  dword[ecx], 0x1
TriggerPFExceptionAfter:
    ret

;------------------------------------------------------------------------------
; ModifyEcxInGlobalBeforeException;
; This function is writed by assebly code because it's only called in this file.
; It's used to set Ecx in mExpectedContextInHandler for different exception.
;------------------------------------------------------------------------------
global ASM_PFX(ModifyEcxInGlobalBeforeException)
ASM_PFX(ModifyEcxInGlobalBeforeException):
    push eax
    lea  eax, [ASM_PFX(mExpectedContextInHandler)]
    mov  [eax + GENERAL_REGISTER_IA32.Ecx], ecx
    pop  eax
    ret

;------------------------------------------------------------------------------
;VOID
;EFIAPI
;AsmTestConsistencyOfCpuContext (
;  IN  EFI_EXCEPTION_TYPE ExceptionType
;  IN  UINTN              FaultParameter   OPTIONAL
;  );
;------------------------------------------------------------------------------
global ASM_PFX(AsmTestConsistencyOfCpuContext)
ASM_PFX(AsmTestConsistencyOfCpuContext):
    ;
    ; push 7 general register plus 4 bytes
    ;
    pushad

    ;
    ; Modify register to mExpectedContextInHandler. Do not handle Esp and Ebp.
    ; CpuExceptionHandlerLib doesn't set Esp and Esp register to the value in SystemContext.
    ;
    lea eax, [ASM_PFX(mExpectedContextInHandler)]
    mov edi, [eax + GENERAL_REGISTER_IA32.Edi]
    mov esi, [eax + GENERAL_REGISTER_IA32.Esi]
    mov ebx, [eax + GENERAL_REGISTER_IA32.Ebx]
    mov edx, [eax + GENERAL_REGISTER_IA32.Edx]
    ;
    ; Set ecx to ExceptionType
    ;
    mov ecx, dword [esp + 0x24]
    mov eax, [eax + GENERAL_REGISTER_IA32.Eax]

    cmp  ecx, 0xd
    jz   GPException
    cmp  ecx, 0xe
    jz   PFException
    jmp  INTnException

PFException:
    mov  ecx, dword [esp + 0x28]                    ; Set ecx to PFAddress.
    call ASM_PFX(ModifyEcxInGlobalBeforeException)  ; Set mExpectedContextInHandler.Ecx to PFAddress.
    push ecx                                        ; Push PfAddress into stack.
    call ASM_PFX(TriggerPFException)
    jmp  AfterException

GPException:
    mov  ecx, dword [esp + 0x28]                    ; Set ecx to CR4_RESERVED_BIT.
    call ASM_PFX(ModifyEcxInGlobalBeforeException)  ; Set mExpectedContextInHandler.Ecx to CR4_RESERVED_BIT.
    push ecx                                        ; Push CR4_RESERVED_BIT into stack.
    call ASM_PFX(TriggerGPException)
    jmp  AfterException

INTnException:
    call ASM_PFX(ModifyEcxInGlobalBeforeException)  ; Set mExpectedContextInHandler.Ecx to ExceptionType.
    push ecx                                        ; Push ExceptionType into stack.
    call ASM_PFX(TriggerINTnException)

AfterException:
    ;
    ; Save register in mActualContextAfterException.
    ;
    push eax
    lea  eax, [ASM_PFX(mActualContextAfterException)]
    mov  [eax + GENERAL_REGISTER_IA32.Edi], edi
    mov  [eax + GENERAL_REGISTER_IA32.Esi], esi
    mov  [eax + GENERAL_REGISTER_IA32.Ebx], ebx
    mov  [eax + GENERAL_REGISTER_IA32.Edx], edx
    mov  [eax + GENERAL_REGISTER_IA32.Ecx], ecx
    pop  ecx
    mov  [eax + GENERAL_REGISTER_IA32.Eax], ecx
    add  esp, 4

    ;
    ; restore original register
    ;
    popad
    ret

;------------------------------------------------------------------------------
; VOID
; EFIAPI
; TriggerStackOverflow (
;  VOID
;  );
;------------------------------------------------------------------------------
global ASM_PFX(TriggerStackOverflow)
ASM_PFX(TriggerStackOverflow):
    lea  ecx, [ASM_PFX(mFaultInstructionLength)]
    mov  dword[ecx], TriggerCpuStackGuardAfter - TriggerCpuStackGuardBefore
TriggerCpuStackGuardBefore:
    ;
    ; Clear CR0.TS since it is set after return from a nested DF
    ;
    call TriggerCpuStackGuardBefore
    clts
TriggerCpuStackGuardAfter:
    ret

;------------------------------------------------------------------------------
; VOID
; EFIAPI
; TriggerINTnException (
;  IN  EFI_EXCEPTION_TYPE ExceptionType
;  );
;------------------------------------------------------------------------------
global ASM_PFX(TriggerINTnException)
ASM_PFX(TriggerINTnException):
    push eax
    push edx
    lea  eax, [AsmTriggerException1 - AsmTriggerException0]
    mov  ecx, dword [esp + 0xc]
    push ecx
    mul  ecx
    mov  ecx, AsmTriggerException0
    add  eax, ecx
    pop  ecx
    pop  edx
    jmp  eax
    ;
    ; eax = AsmTriggerException0 + (AsmTriggerException1 - AsmTriggerException0) * ecx
    ;
%assign Vector 0
%rep  22
AsmTriggerException %+ Vector:
    pop eax
    INT Vector
    ret
%assign Vector Vector+1
%endrep