]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/CpuDxe/AArch64/ExceptionSupport.S
ArmPkg: Tidy assembler code
[mirror_edk2.git] / ArmPkg / Drivers / CpuDxe / AArch64 / ExceptionSupport.S
CommitLineData
25402f5d
HL
1//\r
2// Copyright (c) 2011 - 2013 ARM LTD. All rights reserved.<BR>\r
3//\r
4// This program and the accompanying materials\r
5// are licensed and made available under the terms and conditions of the BSD License\r
6// which accompanies this distribution. The full text of the license may be found at\r
7// http://opensource.org/licenses/bsd-license.php\r
8//\r
9// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11//\r
12//------------------------------------------------------------------------------\r
13\r
14#include <Library/PcdLib.h>\r
15#include <AsmMacroIoLibV8.h>\r
16\r
17/*\r
18 This is the stack constructed by the exception handler (low address to high address).\r
19 X0 to FAR makes up the EFI_SYSTEM_CONTEXT for AArch64.\r
20\r
21 UINT64 X0; 0x000\r
22 UINT64 X1; 0x008\r
23 UINT64 X2; 0x010\r
24 UINT64 X3; 0x018\r
25 UINT64 X4; 0x020\r
26 UINT64 X5; 0x028\r
27 UINT64 X6; 0x030\r
28 UINT64 X7; 0x038\r
29 UINT64 X8; 0x040\r
30 UINT64 X9; 0x048\r
31 UINT64 X10; 0x050\r
32 UINT64 X11; 0x058\r
33 UINT64 X12; 0x060\r
34 UINT64 X13; 0x068\r
35 UINT64 X14; 0x070\r
36 UINT64 X15; 0x078\r
37 UINT64 X16; 0x080\r
38 UINT64 X17; 0x088\r
39 UINT64 X18; 0x090\r
40 UINT64 X19; 0x098\r
41 UINT64 X20; 0x0a0\r
42 UINT64 X21; 0x0a8\r
43 UINT64 X22; 0x0b0\r
44 UINT64 X23; 0x0b8\r
45 UINT64 X24; 0x0c0\r
46 UINT64 X25; 0x0c8\r
47 UINT64 X26; 0x0d0\r
48 UINT64 X27; 0x0d8\r
49 UINT64 X28; 0x0e0\r
50 UINT64 FP; 0x0e8 // x29 - Frame Pointer\r
51 UINT64 LR; 0x0f0 // x30 - Link Register\r
52 UINT64 SP; 0x0f8 // x31 - Stack Pointer\r
53\r
54 // FP/SIMD Registers. 128bit if used as Q-regs.\r
55 UINT64 V0[2]; 0x100\r
56 UINT64 V1[2]; 0x110\r
57 UINT64 V2[2]; 0x120\r
58 UINT64 V3[2]; 0x130\r
59 UINT64 V4[2]; 0x140\r
60 UINT64 V5[2]; 0x150\r
61 UINT64 V6[2]; 0x160\r
62 UINT64 V7[2]; 0x170\r
63 UINT64 V8[2]; 0x180\r
64 UINT64 V9[2]; 0x190\r
65 UINT64 V10[2]; 0x1a0\r
66 UINT64 V11[2]; 0x1b0\r
67 UINT64 V12[2]; 0x1c0\r
68 UINT64 V13[2]; 0x1d0\r
69 UINT64 V14[2]; 0x1e0\r
70 UINT64 V15[2]; 0x1f0\r
71 UINT64 V16[2]; 0x200\r
72 UINT64 V17[2]; 0x210\r
73 UINT64 V18[2]; 0x220\r
74 UINT64 V19[2]; 0x230\r
75 UINT64 V20[2]; 0x240\r
76 UINT64 V21[2]; 0x250\r
77 UINT64 V22[2]; 0x260\r
78 UINT64 V23[2]; 0x270\r
79 UINT64 V24[2]; 0x280\r
80 UINT64 V25[2]; 0x290\r
81 UINT64 V26[2]; 0x2a0\r
82 UINT64 V27[2]; 0x2b0\r
83 UINT64 V28[2]; 0x2c0\r
84 UINT64 V29[2]; 0x2d0\r
85 UINT64 V30[2]; 0x2e0\r
86 UINT64 V31[2]; 0x2f0\r
87\r
88 // System Context\r
89 UINT64 ELR; 0x300 // Exception Link Register\r
90 UINT64 SPSR; 0x308 // Saved Processor Status Register\r
91 UINT64 FPSR; 0x310 // Floating Point Status Register\r
92 UINT64 ESR; 0x318 // EL1 Fault Address Register\r
93 UINT64 FAR; 0x320 // EL1 Exception syndrome register\r
94 UINT64 Padding;0x328 // Required for stack alignment\r
95*/\r
96\r
97ASM_GLOBAL ASM_PFX(ExceptionHandlersStart)\r
98ASM_GLOBAL ASM_PFX(ExceptionHandlersEnd)\r
99ASM_GLOBAL ASM_PFX(CommonExceptionEntry)\r
100ASM_GLOBAL ASM_PFX(AsmCommonExceptionEntry)\r
101ASM_GLOBAL ASM_PFX(CommonCExceptionHandler)\r
102\r
103.text\r
104.align 11\r
105\r
106#define GP_CONTEXT_SIZE (32 * 8)\r
107#define FP_CONTEXT_SIZE (32 * 16)\r
108#define SYS_CONTEXT_SIZE ( 6 * 8) // 5 SYS regs + Alignment requirement (ie: the stack must be aligned on 0x10)\r
109\r
110// Cannot str x31 directly\r
111#define ALL_GP_REGS \\r
112 REG_PAIR (x0, x1, 0x000, GP_CONTEXT_SIZE); \\r
113 REG_PAIR (x2, x3, 0x010, GP_CONTEXT_SIZE); \\r
114 REG_PAIR (x4, x5, 0x020, GP_CONTEXT_SIZE); \\r
115 REG_PAIR (x6, x7, 0x030, GP_CONTEXT_SIZE); \\r
116 REG_PAIR (x8, x9, 0x040, GP_CONTEXT_SIZE); \\r
117 REG_PAIR (x10, x11, 0x050, GP_CONTEXT_SIZE); \\r
118 REG_PAIR (x12, x13, 0x060, GP_CONTEXT_SIZE); \\r
119 REG_PAIR (x14, x15, 0x070, GP_CONTEXT_SIZE); \\r
120 REG_PAIR (x16, x17, 0x080, GP_CONTEXT_SIZE); \\r
121 REG_PAIR (x18, x19, 0x090, GP_CONTEXT_SIZE); \\r
122 REG_PAIR (x20, x21, 0x0a0, GP_CONTEXT_SIZE); \\r
123 REG_PAIR (x22, x23, 0x0b0, GP_CONTEXT_SIZE); \\r
124 REG_PAIR (x24, x25, 0x0c0, GP_CONTEXT_SIZE); \\r
125 REG_PAIR (x26, x27, 0x0d0, GP_CONTEXT_SIZE); \\r
126 REG_PAIR (x28, x29, 0x0e0, GP_CONTEXT_SIZE); \\r
127 REG_ONE (x30, 0x0f0, GP_CONTEXT_SIZE);\r
128\r
129// In order to save the SP we need to put it somwhere else first.\r
130// STR only works with XZR/WZR directly\r
131#define SAVE_SP \\r
132 add x1, sp, FP_CONTEXT_SIZE + SYS_CONTEXT_SIZE; \\r
133 REG_ONE (x1, 0x0f8, GP_CONTEXT_SIZE);\r
134\r
135#define ALL_FP_REGS \\r
136 REG_PAIR (q0, q1, 0x000, FP_CONTEXT_SIZE); \\r
137 REG_PAIR (q2, q3, 0x020, FP_CONTEXT_SIZE); \\r
138 REG_PAIR (q4, q5, 0x040, FP_CONTEXT_SIZE); \\r
139 REG_PAIR (q6, q7, 0x060, FP_CONTEXT_SIZE); \\r
140 REG_PAIR (q8, q9, 0x080, FP_CONTEXT_SIZE); \\r
141 REG_PAIR (q10, q11, 0x0a0, FP_CONTEXT_SIZE); \\r
142 REG_PAIR (q12, q13, 0x0c0, FP_CONTEXT_SIZE); \\r
143 REG_PAIR (q14, q15, 0x0e0, FP_CONTEXT_SIZE); \\r
144 REG_PAIR (q16, q17, 0x100, FP_CONTEXT_SIZE); \\r
145 REG_PAIR (q18, q19, 0x120, FP_CONTEXT_SIZE); \\r
146 REG_PAIR (q20, q21, 0x140, FP_CONTEXT_SIZE); \\r
147 REG_PAIR (q22, q23, 0x160, FP_CONTEXT_SIZE); \\r
148 REG_PAIR (q24, q25, 0x180, FP_CONTEXT_SIZE); \\r
149 REG_PAIR (q26, q27, 0x1a0, FP_CONTEXT_SIZE); \\r
150 REG_PAIR (q28, q29, 0x1c0, FP_CONTEXT_SIZE); \\r
151 REG_PAIR (q30, q31, 0x1e0, FP_CONTEXT_SIZE);\r
152\r
153#define ALL_SYS_REGS \\r
154 REG_PAIR (x1, x2, 0x000, SYS_CONTEXT_SIZE); \\r
155 REG_PAIR (x3, x4, 0x010, SYS_CONTEXT_SIZE); \\r
156 REG_ONE (x5, 0x020, SYS_CONTEXT_SIZE);\r
157\r
158//\r
159// This code gets copied to the ARM vector table\r
160// VectorTableStart - VectorTableEnd gets copied\r
161//\r
162ASM_PFX(ExceptionHandlersStart):\r
163\r
164//\r
165// Current EL with SP0 : 0x0 - 0x180\r
166//\r
167.align 7\r
168ASM_PFX(SynchronousExceptionSP0):\r
169 b ASM_PFX(SynchronousExceptionEntry)\r
170\r
171.align 7\r
172ASM_PFX(IrqSP0):\r
173 b ASM_PFX(IrqEntry)\r
174\r
175.align 7\r
176ASM_PFX(FiqSP0):\r
177 b ASM_PFX(FiqEntry)\r
178\r
179.align 7\r
180ASM_PFX(SErrorSP0):\r
181 b ASM_PFX(SErrorEntry)\r
182\r
183//\r
184// Current EL with SPx: 0x200 - 0x380\r
185//\r
186.align 7\r
187ASM_PFX(SynchronousExceptionSPx):\r
188 b ASM_PFX(SynchronousExceptionEntry)\r
189\r
190.align 7\r
191ASM_PFX(IrqSPx):\r
192 b ASM_PFX(IrqEntry)\r
193\r
194.align 7\r
195ASM_PFX(FiqSPx):\r
196 b ASM_PFX(FiqEntry)\r
197\r
198.align 7\r
199ASM_PFX(SErrorSPx):\r
200 b ASM_PFX(SErrorEntry)\r
201\r
202//\r
203// Lower EL using AArch64 : 0x400 - 0x580\r
204//\r
205.align 7\r
206ASM_PFX(SynchronousExceptionA64):\r
207 b ASM_PFX(SynchronousExceptionEntry)\r
208\r
209.align 7\r
210ASM_PFX(IrqA64):\r
211 b ASM_PFX(IrqEntry)\r
212\r
213.align 7\r
214ASM_PFX(FiqA64):\r
215 b ASM_PFX(FiqEntry)\r
216\r
217.align 7\r
218ASM_PFX(SErrorA64):\r
219 b ASM_PFX(SErrorEntry)\r
220\r
221//\r
222// Lower EL using AArch32 : 0x0 - 0x180\r
223//\r
224.align 7\r
225ASM_PFX(SynchronousExceptionA32):\r
226 b ASM_PFX(SynchronousExceptionEntry)\r
227\r
228.align 7\r
229ASM_PFX(IrqA32):\r
230 b ASM_PFX(IrqEntry)\r
231\r
232.align 7\r
233ASM_PFX(FiqA32):\r
234 b ASM_PFX(FiqEntry)\r
235\r
236.align 7\r
237ASM_PFX(SErrorA32):\r
238 b ASM_PFX(SErrorEntry)\r
239\r
240\r
241#undef REG_PAIR\r
242#undef REG_ONE\r
243#define REG_PAIR(REG1, REG2, OFFSET, CONTEXT_SIZE) stp REG1, REG2, [sp, #(OFFSET-CONTEXT_SIZE)]\r
244#define REG_ONE(REG1, OFFSET, CONTEXT_SIZE) str REG1, [sp, #(OFFSET-CONTEXT_SIZE)]\r
245\r
246ASM_PFX(SynchronousExceptionEntry):\r
247 // Move the stackpointer so we can reach our structure with the str instruction.\r
248 sub sp, sp, FP_CONTEXT_SIZE + SYS_CONTEXT_SIZE\r
249\r
250 // Save all the General regs before touching x0 and x1.\r
251 // This does not save r31(SP) as it is special. We do that later.\r
252 ALL_GP_REGS\r
253\r
254 // Record the tipe of exception that occured.\r
255 mov x0, #EXCEPT_AARCH64_SYNCHRONOUS_EXCEPTIONS\r
256\r
257 // Jump to our general handler to deal with all the common parts and process the exception.\r
258 ldr x1, ASM_PFX(CommonExceptionEntry)\r
259 br x1\r
260\r
261ASM_PFX(IrqEntry):\r
262 sub sp, sp, FP_CONTEXT_SIZE + SYS_CONTEXT_SIZE\r
263 ALL_GP_REGS\r
264 mov x0, #EXCEPT_AARCH64_IRQ\r
265 ldr x1, ASM_PFX(CommonExceptionEntry)\r
266 br x1\r
267\r
268ASM_PFX(FiqEntry):\r
269 sub sp, sp, FP_CONTEXT_SIZE + SYS_CONTEXT_SIZE\r
270 ALL_GP_REGS\r
271 mov x0, #EXCEPT_AARCH64_FIQ\r
272 ldr x1, ASM_PFX(CommonExceptionEntry)\r
273 br x1\r
274\r
275ASM_PFX(SErrorEntry):\r
276 sub sp, sp, FP_CONTEXT_SIZE + SYS_CONTEXT_SIZE\r
277 ALL_GP_REGS\r
278 mov x0, #EXCEPT_AARCH64_SERROR\r
279 ldr x1, ASM_PFX(CommonExceptionEntry)\r
280 br x1\r
281\r
282\r
283//\r
284// This gets patched by the C code that patches in the vector table\r
285//\r
286.align 3\r
287ASM_PFX(CommonExceptionEntry):\r
288 .dword ASM_PFX(AsmCommonExceptionEntry)\r
289\r
290ASM_PFX(ExceptionHandlersEnd):\r
291\r
292\r
293\r
294//\r
295// This code runs from CpuDxe driver loaded address. It is patched into\r
296// CommonExceptionEntry.\r
297//\r
298ASM_PFX(AsmCommonExceptionEntry):\r
299 /* NOTE:\r
300 We have to break up the save code because the immidiate value to be used\r
301 with the SP is to big to do it all in one step so we need to shuffle the SP\r
302 along as we go. (we only have 9bits of immediate to work with) */\r
303\r
304 // Save the current Stack pointer before we start modifying it.\r
305 SAVE_SP\r
306\r
307 // Preserve the stack pointer we came in with before we modify it\r
308 EL1_OR_EL2(x1)\r
3091:mrs x1, elr_el1 // Exception Link Register\r
310 mrs x2, spsr_el1 // Saved Processor Status Register 32bit\r
311 mrs x3, fpsr // Floating point Status Register 32bit\r
312 mrs x4, esr_el1 // EL1 Exception syndrome register 32bit\r
313 mrs x5, far_el1 // EL1 Fault Address Register\r
314 b 3f\r
315\r
3162:mrs x1, elr_el2 // Exception Link Register\r
317 mrs x2, spsr_el2 // Saved Processor Status Register 32bit\r
318 mrs x3, fpsr // Floating point Status Register 32bit\r
319 mrs x4, esr_el2 // EL1 Exception syndrome register 32bit\r
320 mrs x5, far_el2 // EL1 Fault Address Register\r
321\r
322 // Adjust SP to save next set\r
3233:add sp, sp, FP_CONTEXT_SIZE\r
324\r
325 // Push FP regs to Stack.\r
326 ALL_FP_REGS\r
327\r
328 // Adjust SP to save next set\r
329 add sp, sp, SYS_CONTEXT_SIZE\r
330\r
331 // Save the SYS regs\r
332 ALL_SYS_REGS\r
333\r
334 // Point to top of struct after all regs saved\r
335 sub sp, sp, GP_CONTEXT_SIZE + FP_CONTEXT_SIZE + SYS_CONTEXT_SIZE\r
336\r
337 // x0 still holds the exception type.\r
338 // Set x1 to point to the top of our struct on the Stack\r
339 mov x1, sp\r
340\r
341// CommonCExceptionHandler (\r
342// IN EFI_EXCEPTION_TYPE ExceptionType, R0\r
343// IN OUT EFI_SYSTEM_CONTEXT SystemContext R1\r
344// )\r
345\r
346 // Call the handler as defined above\r
347\r
348 // For now we spin in the handler if we received an abort of some kind.\r
349 // We do not try to recover.\r
350 bl ASM_PFX(CommonCExceptionHandler) // Call exception handler\r
351\r
352\r
353// Defines for popping from stack\r
354\r
355#undef REG_PAIR\r
356#undef REG_ONE\r
357#define REG_PAIR(REG1, REG2, OFFSET, CONTEXT_SIZE) ldp REG1, REG2, [sp, #(OFFSET-CONTEXT_SIZE)]\r
358\r
359#define REG_ONE(REG1, OFFSET, CONTEXT_SIZE) ldr REG1, [sp, #(OFFSET-CONTEXT_SIZE)]\r
360\r
361\r
362 // pop all regs and return from exception.\r
363 add sp, sp, GP_CONTEXT_SIZE\r
364 ALL_GP_REGS\r
365\r
366 // Adjust SP to pop next set\r
367 add sp, sp, FP_CONTEXT_SIZE\r
368 // Pop FP regs to Stack.\r
369 ALL_FP_REGS\r
370\r
371 // Adjust SP to be where we started from when we came into the handler.\r
372 // The handler can not change the SP.\r
373 add sp, sp, SYS_CONTEXT_SIZE\r
374\r
375 eret\r
376\r
377#undef REG_PAIR\r
378#undef REG_ONE\r