1 #------------------------------------------------------------------------------
3 # Use ARMv6 instruction to operate on a single stack
5 # Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
6 # Copyright (c) 2014, ARM Limited. All rights reserved.<BR>
7 # Copyright (c) 2016 HP Development Company, L.P.<BR>
9 # This program and the accompanying materials
10 # are licensed and made available under the terms and conditions of the BSD License
11 # which accompanies this distribution. The full text of the license may be found at
12 # http://opensource.org/licenses/bsd-license.php
14 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #------------------------------------------------------------------------------
19 #include <Library/PcdLib.h>
23 This is the stack constructed by the exception handler (low address to high address)
24 # R0 - IFAR is EFI_SYSTEM_CONTEXT for ARM
27 R0 0x00 # stmfd SP!,{R0-R12}
40 SP 0x34 # reserved via subtraction 0x20 (32) from SP
49 LR 0x54 # SVC Link register (we need to restore it)
51 LR 0x58 # pushed by srsfd
57 GCC_ASM_EXPORT(ExceptionHandlersStart)
58 GCC_ASM_EXPORT(ExceptionHandlersEnd)
59 GCC_ASM_EXPORT(CommonExceptionEntry)
60 GCC_ASM_EXPORT(AsmCommonExceptionEntry)
61 GCC_ASM_EXPORT(CommonCExceptionHandler)
65 #if !defined(__APPLE__)
66 .fpu neon @ makes vpush/vpop assemble
72 // This code gets copied to the ARM vector table
73 // ExceptionHandlersStart - ExceptionHandlersEnd gets copied
75 ASM_PFX(ExceptionHandlersStart):
80 ASM_PFX(UndefinedInstruction):
81 b ASM_PFX(UndefinedInstructionEntry)
83 ASM_PFX(SoftwareInterrupt):
84 b ASM_PFX(SoftwareInterruptEntry)
86 ASM_PFX(PrefetchAbort):
87 b ASM_PFX(PrefetchAbortEntry)
90 b ASM_PFX(DataAbortEntry)
92 ASM_PFX(ReservedException):
93 b ASM_PFX(ReservedExceptionEntry)
102 srsdb #0x13! @ Store return state on SVC stack
103 @ We are already in SVC mode
105 stmfd SP!,{LR} @ Store the link register for the current mode
106 sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
107 stmfd SP!,{R0-R12} @ Store the register state
109 mov R0,#0 @ ExceptionType
110 ldr R1,ASM_PFX(CommonExceptionEntry)
113 ASM_PFX(UndefinedInstructionEntry):
114 sub LR, LR, #4 @ Only -2 for Thumb, adjust in CommonExceptionEntry
115 srsdb #0x13! @ Store return state on SVC stack
116 cps #0x13 @ Switch to SVC for common stack
117 stmfd SP!,{LR} @ Store the link register for the current mode
118 sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
119 stmfd SP!,{R0-R12} @ Store the register state
121 mov R0,#1 @ ExceptionType
122 ldr R1,ASM_PFX(CommonExceptionEntry)
125 ASM_PFX(SoftwareInterruptEntry):
126 srsdb #0x13! @ Store return state on SVC stack
127 @ We are already in SVC mode
128 stmfd SP!,{LR} @ Store the link register for the current mode
129 sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
130 stmfd SP!,{R0-R12} @ Store the register state
132 mov R0,#2 @ ExceptionType
133 ldr R1,ASM_PFX(CommonExceptionEntry)
136 ASM_PFX(PrefetchAbortEntry):
138 srsdb #0x13! @ Store return state on SVC stack
139 cps #0x13 @ Switch to SVC for common stack
140 stmfd SP!,{LR} @ Store the link register for the current mode
141 sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
142 stmfd SP!,{R0-R12} @ Store the register state
144 mov R0,#3 @ ExceptionType
145 ldr R1,ASM_PFX(CommonExceptionEntry)
148 ASM_PFX(DataAbortEntry):
150 srsdb #0x13! @ Store return state on SVC stack
151 cps #0x13 @ Switch to SVC for common stack
152 stmfd SP!,{LR} @ Store the link register for the current mode
153 sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
154 stmfd SP!,{R0-R12} @ Store the register state
157 ldr R1,ASM_PFX(CommonExceptionEntry)
160 ASM_PFX(ReservedExceptionEntry):
161 srsdb #0x13! @ Store return state on SVC stack
162 cps #0x13 @ Switch to SVC for common stack
163 stmfd SP!,{LR} @ Store the link register for the current mode
164 sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
165 stmfd SP!,{R0-R12} @ Store the register state
168 ldr R1,ASM_PFX(CommonExceptionEntry)
173 srsdb #0x13! @ Store return state on SVC stack
174 cps #0x13 @ Switch to SVC for common stack
175 stmfd SP!,{LR} @ Store the link register for the current mode
176 sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
177 stmfd SP!,{R0-R12} @ Store the register state
179 mov R0,#6 @ ExceptionType
180 ldr R1,ASM_PFX(CommonExceptionEntry)
185 srsdb #0x13! @ Store return state on SVC stack
186 cps #0x13 @ Switch to SVC for common stack
187 stmfd SP!,{LR} @ Store the link register for the current mode
188 sub SP,SP,#0x20 @ Save space for SP, LR, PC, IFAR - CPSR
189 stmfd SP!,{R0-R12} @ Store the register state
190 @ Since we have already switch to SVC R8_fiq - R12_fiq
191 @ never get used or saved
192 mov R0,#7 @ ExceptionType
193 ldr R1,ASM_PFX(CommonExceptionEntry)
197 // This gets patched by the C code that patches in the vector table
199 ASM_PFX(CommonExceptionEntry):
200 .word ASM_PFX(AsmCommonExceptionEntry)
202 ASM_PFX(ExceptionHandlersEnd):
205 // This code runs from CpuDxe driver loaded address. It is patched into
206 // CommonExceptionEntry.
208 ASM_PFX(AsmCommonExceptionEntry):
209 mrc p15, 0, R1, c6, c0, 2 @ Read IFAR
210 str R1, [SP, #0x50] @ Store it in EFI_SYSTEM_CONTEXT_ARM.IFAR
212 mrc p15, 0, R1, c5, c0, 1 @ Read IFSR
213 str R1, [SP, #0x4c] @ Store it in EFI_SYSTEM_CONTEXT_ARM.IFSR
215 mrc p15, 0, R1, c6, c0, 0 @ Read DFAR
216 str R1, [SP, #0x48] @ Store it in EFI_SYSTEM_CONTEXT_ARM.DFAR
218 mrc p15, 0, R1, c5, c0, 0 @ Read DFSR
219 str R1, [SP, #0x44] @ Store it in EFI_SYSTEM_CONTEXT_ARM.DFSR
221 ldr R1, [SP, #0x5c] @ srsdb saved pre-exception CPSR on the stack
222 str R1, [SP, #0x40] @ Store it in EFI_SYSTEM_CONTEXT_ARM.CPSR
224 add R2, SP, #0x38 @ Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR
225 and R3, R1, #0x1f @ Check CPSR to see if User or System Mode
226 cmp R3, #0x1f @ if ((CPSR == 0x10) || (CPSR == 0x1f))
228 stmdaeq R2, {lr}^ @ save unbanked lr
230 stmdane R2, {lr} @ save SVC lr
233 ldr R5, [SP, #0x58] @ PC is the LR pushed by srsfd
234 @ Check to see if we have to adjust for Thumb entry
235 sub r4, r0, #1 @ if (ExceptionType == 1 || ExceptionType == 2)) {
236 cmp r4, #1 @ // UND & SVC have differnt LR adjust for Thumb
239 tst r1, #0x20 @ if ((CPSR & T)) == T) { // Thumb Mode on entry
240 addne R5, R5, #2 @ PC += 2;
241 strne R5,[SP,#0x58] @ Update LR value pushed by srsfd
245 str R5, [SP, #0x3c] @ Store it in EFI_SYSTEM_CONTEXT_ARM.PC
247 add R1, SP, #0x60 @ We pushed 0x60 bytes on the stack
248 str R1, [SP, #0x34] @ Store it in EFI_SYSTEM_CONTEXT_ARM.SP
250 @ R0 is ExceptionType
251 mov R1,SP @ R1 is SystemContext
253 #if (FixedPcdGet32(PcdVFPEnabled))
254 vpush {d0-d15} @ save vstm registers in case they are used in optimizations
257 mov R4, SP @ Save current SP
259 subne SP, SP, #4 @ Adjust SP if not 8-byte aligned
264 CommonCExceptionHandler (
265 IN EFI_EXCEPTION_TYPE ExceptionType, R0
266 IN OUT EFI_SYSTEM_CONTEXT SystemContext R1
270 blx ASM_PFX(CommonCExceptionHandler) @ Call exception handler
272 mov SP, R4 @ Restore SP
274 #if (FixedPcdGet32(PcdVFPEnabled))
278 ldr R1, [SP, #0x4c] @ Restore EFI_SYSTEM_CONTEXT_ARM.IFSR
279 mcr p15, 0, R1, c5, c0, 1 @ Write IFSR
281 ldr R1, [SP, #0x44] @ Restore EFI_SYSTEM_CONTEXT_ARM.DFSR
282 mcr p15, 0, R1, c5, c0, 0 @ Write DFSR
284 ldr R1,[SP,#0x3c] @ EFI_SYSTEM_CONTEXT_ARM.PC
285 str R1,[SP,#0x58] @ Store it back to srsfd stack slot so it can be restored
287 ldr R1,[SP,#0x40] @ EFI_SYSTEM_CONTEXT_ARM.CPSR
288 str R1,[SP,#0x5c] @ Store it back to srsfd stack slot so it can be restored
290 add R3, SP, #0x54 @ Make R3 point to SVC LR saved on entry
291 add R2, SP, #0x38 @ Make R2 point to EFI_SYSTEM_CONTEXT_ARM.LR
292 and R1, R1, #0x1f @ Check to see if User or System Mode
293 cmp R1, #0x1f @ if ((CPSR == 0x10) || (CPSR == 0x1f))
295 ldmibeq R2, {lr}^ @ restore unbanked lr
297 ldmibne R3, {lr} @ restore SVC lr, via ldmfd SP!, {LR}
299 ldmfd SP!,{R0-R12} @ Restore general purpose registers
300 @ Exception handler can not change SP
302 add SP,SP,#0x20 @ Clear out the remaining stack space
303 ldmfd SP!,{LR} @ restore the link register for this context
304 rfefd SP! @ return from exception via srsfd stack slot