summaryrefslogtreecommitdiff
path: root/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/X64/ArchExceptionHandlerTestAsm.nasm
blob: e229dbed00c371fdabb48ecfc3b643acb6990efe (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
;------------------------------------------------------------------------------
;
; Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
; SPDX-License-Identifier: BSD-2-Clause-Patent
;
; Module Name:
;
;   ArchExceptionHandlerTestAsm.nasm
;
; Abstract:
;
;   x64 CPU Exception Handler Lib Unit test
;
;------------------------------------------------------------------------------

    DEFAULT REL
    SECTION .text

struc GENERAL_REGISTER
  .Rdi:    resq    1
  .Rsi:    resq    1
  .Rbx:    resq    1
  .Rdx:    resq    1
  .Rcx:    resq    1
  .Rax:    resq    1
  .R8:     resq    1
  .R9:     resq    1
  .R10:    resq    1
  .R11:    resq    1
  .R12:    resq    1
  .R13:    resq    1
  .R14:    resq    1
  .R15:    resq    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
    ;
    push rcx
    lea  rcx, [ASM_PFX(mFaultInstructionLength)]
    mov  qword[rcx], TriggerGPExceptionAfter - TriggerGPExceptionBefore
    pop  rcx
TriggerGPExceptionBefore:
    mov  cr4, rcx
TriggerGPExceptionAfter:
    ret

;------------------------------------------------------------------------------
; VOID
; EFIAPI
; TriggerPFException (
;  UINTN  PFAddress
;  );
;------------------------------------------------------------------------------
global ASM_PFX(TriggerPFException)
ASM_PFX(TriggerPFException):
    push rcx
    lea  rcx, [ASM_PFX(mFaultInstructionLength)]
    mov  qword[rcx], TriggerPFExceptionAfter - TriggerPFExceptionBefore
    pop  rcx
TriggerPFExceptionBefore:
    mov  qword[rcx], 0x1
TriggerPFExceptionAfter:
    ret

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

;------------------------------------------------------------------------------
;VOID
;EFIAPI
;AsmTestConsistencyOfCpuContext (
;  IN  EFI_EXCEPTION_TYPE ExceptionType
;  IN  UINTN              FaultParameter   OPTIONAL
;  );
;------------------------------------------------------------------------------
global ASM_PFX(AsmTestConsistencyOfCpuContext)
ASM_PFX(AsmTestConsistencyOfCpuContext):
    ;
    ; Push original register
    ;
    push r15
    push r14
    push r13
    push r12
    push r11
    push r10
    push r9
    push r8
    push rax
    push rcx
    push rbx
    push rsi
    push rdi
    push rdx
    push rdx

    ;
    ; Modify registers to mExpectedContextInHandler. Do not handle Rsp and Rbp.
    ; CpuExceptionHandlerLib doesn't set Rsp and Rsp register to the value in SystemContext.
    ;
    lea r15, [ASM_PFX(mExpectedContextInHandler)]
    mov rdi, [r15 + GENERAL_REGISTER.Rdi]
    mov rsi, [r15 + GENERAL_REGISTER.Rsi]
    mov rbx, [r15 + GENERAL_REGISTER.Rbx]
    mov rdx, [r15 + GENERAL_REGISTER.Rdx]
    mov rax, [r15 + GENERAL_REGISTER.Rax]
    mov r8,  [r15 + GENERAL_REGISTER.R8]
    mov r9,  [r15 + GENERAL_REGISTER.R9]
    mov r10, [r15 + GENERAL_REGISTER.R10]
    mov r11, [r15 + GENERAL_REGISTER.R11]
    mov r12, [r15 + GENERAL_REGISTER.R12]
    mov r13, [r15 + GENERAL_REGISTER.R13]
    mov r14, [r15 + GENERAL_REGISTER.R14]
    mov r15, [r15 + GENERAL_REGISTER.R15]

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

PFException:
    pop  rcx                                       ; Pop rdx(PFAddress) to rcx.
    call ASM_PFX(ModifyRcxInGlobalBeforeException) ; Set mExpectedContextInHandler.Rcx to PFAddress.
    call ASM_PFX(TriggerPFException)
    jmp  AfterException

GPException:
    pop  rcx                                       ; Pop rdx(Cr4ReservedBit) to rcx.
    call ASM_PFX(ModifyRcxInGlobalBeforeException) ; Set mExpectedContextInHandler.Rcx to Cr4ReservedBit.
    call ASM_PFX(TriggerGPException)
    jmp  AfterException

INTnException:
    ;
    ; Modify Rcx in mExpectedContextInHandler.
    ;
    add  Rsp, 8                                    ; Discard the extra Rdx in stack. Rcx is ExceptionType now.
    call ASM_PFX(ModifyRcxInGlobalBeforeException) ; Set mExpectedContextInHandler.Rcx to ExceptionType.
    call ASM_PFX(TriggerINTnException)

AfterException:
    ;
    ; Save registers in mActualContextAfterException
    ;
    push rax
    lea  rax, [ASM_PFX(mActualContextAfterException)]
    mov  [rax + GENERAL_REGISTER.Rdi], rdi
    mov  [rax + GENERAL_REGISTER.Rsi], rsi
    mov  [rax + GENERAL_REGISTER.Rbx], rbx
    mov  [rax + GENERAL_REGISTER.Rdx], rdx
    mov  [rax + GENERAL_REGISTER.Rcx], rcx
    pop  rcx
    mov  [rax + GENERAL_REGISTER.Rax], rcx
    mov  [rax + GENERAL_REGISTER.R8],  r8
    mov  [rax + GENERAL_REGISTER.R9],  r9
    mov  [rax + GENERAL_REGISTER.R10], r10
    mov  [rax + GENERAL_REGISTER.R11], r11
    mov  [rax + GENERAL_REGISTER.R12], r12
    mov  [rax + GENERAL_REGISTER.R13], r13
    mov  [rax + GENERAL_REGISTER.R14], r14
    mov  [rax + GENERAL_REGISTER.R15], r15

    ;
    ; restore original register
    ;
    pop rdx
    pop rdi
    pop rsi
    pop rbx
    pop rcx
    pop rax
    pop r8
    pop r9
    pop r10
    pop r11
    pop r12
    pop r13
    pop r14
    pop r15

    ret

;------------------------------------------------------------------------------
; VOID
; EFIAPI
; TriggerStackOverflow (
;  VOID
;  );
;------------------------------------------------------------------------------
global ASM_PFX(TriggerStackOverflow)
ASM_PFX(TriggerStackOverflow):
    push rcx
    lea  rcx, [ASM_PFX(mFaultInstructionLength)]
    mov  qword[rcx], TriggerCpuStackGuardAfter - TriggerCpuStackGuardBefore
    pop  rcx
TriggerCpuStackGuardBefore:
    call TriggerCpuStackGuardBefore
TriggerCpuStackGuardAfter:
    ret

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