blob: b6dcccb756870a416a093c202338bcae53d88c6f [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <arch.h>
Dan Handley35e98e52014-04-09 13:13:04 +010032#include <asm_macros.S>
33#include <cm_macros.S>
Dan Handley97043ac2014-04-09 13:14:54 +010034#include <context.h>
35#include <platform.h>
36#include <runtime_svc.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010037
38 .globl runtime_exceptions
Jeenu Viswambharancaa84932014-02-06 10:36:15 +000039 .globl el3_exit
Achin Gupta4f6ad662013-10-25 09:08:21 +010040
Soby Mathewc3260f92014-04-30 15:36:37 +010041 .macro save_x18_to_x29_sp_el0
42 stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
43 stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
44 stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
45 stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
46 stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
47 stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
48 mrs x18, sp_el0
49 str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
50 .endm
51
Achin Guptab739f222014-01-18 16:50:09 +000052 .section .vectors, "ax"; .align 11
53
Achin Gupta4f6ad662013-10-25 09:08:21 +010054 .align 7
55runtime_exceptions:
56 /* -----------------------------------------------------
57 * Current EL with _sp_el0 : 0x0 - 0x180
58 * -----------------------------------------------------
59 */
60sync_exception_sp_el0:
Jeenu Viswambharancaa84932014-02-06 10:36:15 +000061 /* -----------------------------------------------------
62 * We don't expect any synchronous exceptions from EL3
63 * -----------------------------------------------------
64 */
Soby Mathewa43d4312014-04-07 15:28:55 +010065 bl dump_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +000066 check_vector_size sync_exception_sp_el0
Achin Gupta4f6ad662013-10-25 09:08:21 +010067
68 .align 7
Jeenu Viswambharancaa84932014-02-06 10:36:15 +000069 /* -----------------------------------------------------
70 * EL3 code is non-reentrant. Any asynchronous exception
71 * is a serious error. Loop infinitely.
72 * -----------------------------------------------------
73 */
Achin Gupta4f6ad662013-10-25 09:08:21 +010074irq_sp_el0:
Soby Mathewa43d4312014-04-07 15:28:55 +010075 bl dump_intr_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +000076 check_vector_size irq_sp_el0
Achin Gupta4f6ad662013-10-25 09:08:21 +010077
78 .align 7
79fiq_sp_el0:
Soby Mathewa43d4312014-04-07 15:28:55 +010080 bl dump_intr_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +000081 check_vector_size fiq_sp_el0
Achin Gupta4f6ad662013-10-25 09:08:21 +010082
83 .align 7
84serror_sp_el0:
Soby Mathewa43d4312014-04-07 15:28:55 +010085 bl dump_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +000086 check_vector_size serror_sp_el0
Achin Gupta4f6ad662013-10-25 09:08:21 +010087
88 /* -----------------------------------------------------
89 * Current EL with SPx: 0x200 - 0x380
90 * -----------------------------------------------------
91 */
92 .align 7
93sync_exception_sp_elx:
Jeenu Viswambharancaa84932014-02-06 10:36:15 +000094 /* -----------------------------------------------------
95 * This exception will trigger if anything went wrong
96 * during a previous exception entry or exit or while
97 * handling an earlier unexpected synchronous exception.
Soby Mathewa43d4312014-04-07 15:28:55 +010098 * There is a high probability that SP_EL3 is corrupted.
Jeenu Viswambharancaa84932014-02-06 10:36:15 +000099 * -----------------------------------------------------
100 */
Soby Mathewa43d4312014-04-07 15:28:55 +0100101 bl dump_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000102 check_vector_size sync_exception_sp_elx
Achin Gupta4f6ad662013-10-25 09:08:21 +0100103
104 .align 7
105irq_sp_elx:
Soby Mathewa43d4312014-04-07 15:28:55 +0100106 bl dump_intr_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000107 check_vector_size irq_sp_elx
108
Achin Gupta4f6ad662013-10-25 09:08:21 +0100109 .align 7
110fiq_sp_elx:
Soby Mathewa43d4312014-04-07 15:28:55 +0100111 bl dump_intr_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000112 check_vector_size fiq_sp_elx
113
Achin Gupta4f6ad662013-10-25 09:08:21 +0100114 .align 7
115serror_sp_elx:
Soby Mathewa43d4312014-04-07 15:28:55 +0100116 bl dump_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000117 check_vector_size serror_sp_elx
Achin Gupta4f6ad662013-10-25 09:08:21 +0100118
119 /* -----------------------------------------------------
120 * Lower EL using AArch64 : 0x400 - 0x580
121 * -----------------------------------------------------
122 */
123 .align 7
124sync_exception_aarch64:
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000125 /* -----------------------------------------------------
126 * This exception vector will be the entry point for
127 * SMCs and traps that are unhandled at lower ELs most
128 * commonly. SP_EL3 should point to a valid cpu context
129 * where the general purpose and system register state
130 * can be saved.
131 * -----------------------------------------------------
132 */
133 handle_sync_exception
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000134 check_vector_size sync_exception_aarch64
Achin Gupta4f6ad662013-10-25 09:08:21 +0100135
136 .align 7
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000137 /* -----------------------------------------------------
138 * Asynchronous exceptions from lower ELs are not
139 * currently supported. Report their occurrence.
140 * -----------------------------------------------------
141 */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100142irq_aarch64:
Soby Mathewa43d4312014-04-07 15:28:55 +0100143 bl dump_intr_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000144 check_vector_size irq_aarch64
Achin Gupta4f6ad662013-10-25 09:08:21 +0100145
146 .align 7
147fiq_aarch64:
Soby Mathewa43d4312014-04-07 15:28:55 +0100148 bl dump_intr_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000149 check_vector_size fiq_aarch64
Achin Gupta4f6ad662013-10-25 09:08:21 +0100150
151 .align 7
152serror_aarch64:
Soby Mathewa43d4312014-04-07 15:28:55 +0100153 bl dump_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000154 check_vector_size serror_aarch64
Achin Gupta4f6ad662013-10-25 09:08:21 +0100155
156 /* -----------------------------------------------------
157 * Lower EL using AArch32 : 0x600 - 0x780
158 * -----------------------------------------------------
159 */
160 .align 7
161sync_exception_aarch32:
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000162 /* -----------------------------------------------------
163 * This exception vector will be the entry point for
164 * SMCs and traps that are unhandled at lower ELs most
165 * commonly. SP_EL3 should point to a valid cpu context
166 * where the general purpose and system register state
167 * can be saved.
168 * -----------------------------------------------------
169 */
170 handle_sync_exception
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000171 check_vector_size sync_exception_aarch32
Achin Gupta4f6ad662013-10-25 09:08:21 +0100172
173 .align 7
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000174 /* -----------------------------------------------------
175 * Asynchronous exceptions from lower ELs are not
176 * currently supported. Report their occurrence.
177 * -----------------------------------------------------
178 */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100179irq_aarch32:
Soby Mathewa43d4312014-04-07 15:28:55 +0100180 bl dump_intr_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000181 check_vector_size irq_aarch32
Achin Gupta4f6ad662013-10-25 09:08:21 +0100182
183 .align 7
184fiq_aarch32:
Soby Mathewa43d4312014-04-07 15:28:55 +0100185 bl dump_intr_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000186 check_vector_size fiq_aarch32
Achin Gupta4f6ad662013-10-25 09:08:21 +0100187
188 .align 7
189serror_aarch32:
Soby Mathewa43d4312014-04-07 15:28:55 +0100190 bl dump_state_and_die
Jeenu Viswambharana7934d62014-02-07 15:53:18 +0000191 check_vector_size serror_aarch32
192
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000193 .align 7
194
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000195 /* -----------------------------------------------------
196 * The following code handles secure monitor calls.
197 * Depending upon the execution state from where the SMC
198 * has been invoked, it frees some general purpose
199 * registers to perform the remaining tasks. They
200 * involve finding the runtime service handler that is
201 * the target of the SMC & switching to runtime stacks
202 * (SP_EL0) before calling the handler.
203 *
204 * Note that x30 has been explicitly saved and can be
205 * used here
206 * -----------------------------------------------------
207 */
Andrew Thoelke0a30cf52014-03-18 13:46:55 +0000208func smc_handler
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000209smc_handler32:
210 /* Check whether aarch32 issued an SMC64 */
211 tbnz x0, #FUNCID_CC_SHIFT, smc_prohibited
212
213 /* -----------------------------------------------------
214 * Since we're are coming from aarch32, x8-x18 need to
215 * be saved as per SMC32 calling convention. If a lower
216 * EL in aarch64 is making an SMC32 call then it must
217 * have saved x8-x17 already therein.
218 * -----------------------------------------------------
219 */
220 stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
221 stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
222 stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
223 stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
224 stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
225
226 /* x4-x7, x18, sp_el0 are saved below */
227
228smc_handler64:
229 /* -----------------------------------------------------
230 * Populate the parameters for the SMC handler. We
231 * already have x0-x4 in place. x5 will point to a
232 * cookie (not used now). x6 will point to the context
233 * structure (SP_EL3) and x7 will contain flags we need
234 * to pass to the handler Hence save x5-x7. Note that x4
235 * only needs to be preserved for AArch32 callers but we
236 * do it for AArch64 callers as well for convenience
237 * -----------------------------------------------------
238 */
239 stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
240 stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
241
Soby Mathewc3260f92014-04-30 15:36:37 +0100242 /* Save rest of the gpregs and sp_el0*/
243 save_x18_to_x29_sp_el0
244
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000245 mov x5, xzr
246 mov x6, sp
247
248 /* Get the unique owning entity number */
249 ubfx x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
250 ubfx x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
251 orr x16, x16, x15, lsl #FUNCID_OEN_WIDTH
252
253 adr x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
254
255 /* Load descriptor index from array of indices */
256 adr x14, rt_svc_descs_indices
257 ldrb w15, [x14, x16]
258
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000259 /* -----------------------------------------------------
260 * Restore the saved C runtime stack value which will
261 * become the new SP_EL0 i.e. EL3 runtime stack. It was
262 * saved in the 'cpu_context' structure prior to the last
263 * ERET from EL3.
264 * -----------------------------------------------------
265 */
266 ldr x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
267
268 /*
269 * Any index greater than 127 is invalid. Check bit 7 for
270 * a valid index
271 */
272 tbnz w15, 7, smc_unknown
273
274 /* Switch to SP_EL0 */
275 msr spsel, #0
276
277 /* -----------------------------------------------------
278 * Get the descriptor using the index
279 * x11 = (base + off), x15 = index
280 *
281 * handler = (base + off) + (index << log2(size))
282 * -----------------------------------------------------
283 */
284 lsl w10, w15, #RT_SVC_SIZE_LOG2
285 ldr x15, [x11, w10, uxtw]
286
287 /* -----------------------------------------------------
288 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there
289 * is a world switch during SMC handling.
290 * TODO: Revisit if all system registers can be saved
291 * later.
292 * -----------------------------------------------------
293 */
294 mrs x16, spsr_el3
295 mrs x17, elr_el3
296 mrs x18, scr_el3
297 stp x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
298 stp x18, xzr, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
299
300 /* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
301 bfi x7, x18, #0, #1
302
303 mov sp, x12
304
305 /* -----------------------------------------------------
306 * Call the Secure Monitor Call handler and then drop
307 * directly into el3_exit() which will program any
308 * remaining architectural state prior to issuing the
309 * ERET to the desired lower EL.
310 * -----------------------------------------------------
311 */
312#if DEBUG
313 cbz x15, rt_svc_fw_critical_error
314#endif
315 blr x15
316
317 /* -----------------------------------------------------
318 * This routine assumes that the SP_EL3 is pointing to
319 * a valid context structure from where the gp regs and
320 * other special registers can be retrieved.
Andrew Thoelke0a30cf52014-03-18 13:46:55 +0000321 *
322 * Keep it in the same section as smc_handler as this
323 * function uses a fall-through to el3_exit
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000324 * -----------------------------------------------------
325 */
326el3_exit: ; .type el3_exit, %function
327 /* -----------------------------------------------------
328 * Save the current SP_EL0 i.e. the EL3 runtime stack
329 * which will be used for handling the next SMC. Then
330 * switch to SP_EL3
331 * -----------------------------------------------------
332 */
333 mov x17, sp
334 msr spsel, #1
335 str x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
336
337 /* -----------------------------------------------------
338 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
339 * -----------------------------------------------------
340 */
341 ldp x18, xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
342 ldp x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
343 msr scr_el3, x18
344 msr spsr_el3, x16
345 msr elr_el3, x17
346
347 /* Restore saved general purpose registers and return */
Soby Mathewa43d4312014-04-07 15:28:55 +0100348 b restore_gp_registers_eret
Achin Gupta4f6ad662013-10-25 09:08:21 +0100349
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000350smc_unknown:
351 /*
352 * Here we restore x4-x18 regardless of where we came from. AArch32
353 * callers will find the registers contents unchanged, but AArch64
354 * callers will find the registers modified (with stale earlier NS
355 * content). Either way, we aren't leaking any secure information
356 * through them
357 */
Soby Mathewa43d4312014-04-07 15:28:55 +0100358 mov w0, #SMC_UNK
359 b restore_gp_registers_callee_eret
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000360
361smc_prohibited:
Soby Mathewc3260f92014-04-30 15:36:37 +0100362 ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000363 mov w0, #SMC_UNK
364 eret
365
366rt_svc_fw_critical_error:
Soby Mathewa43d4312014-04-07 15:28:55 +0100367 msr spsel, #1 /* Switch to SP_ELx */
368 bl dump_state_and_die
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000369
370 /* -----------------------------------------------------
371 * The following functions are used to saved and restore
Soby Mathewc3260f92014-04-30 15:36:37 +0100372 * all the general pupose registers. Ideally we would
373 * only save and restore the callee saved registers when
374 * a world switch occurs but that type of implementation
375 * is more complex. So currently we will always save and
376 * restore these registers on entry and exit of EL3.
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000377 * These are not macros to ensure their invocation fits
378 * within the 32 instructions per exception vector.
379 * -----------------------------------------------------
380 */
Soby Mathewc3260f92014-04-30 15:36:37 +0100381func save_gp_registers
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000382 stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
383 stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
384 stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
385 stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
386 stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
387 stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
388 stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
389 stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
390 stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
Soby Mathewc3260f92014-04-30 15:36:37 +0100391 save_x18_to_x29_sp_el0
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000392 ret
393
Soby Mathewa43d4312014-04-07 15:28:55 +0100394func restore_gp_registers_eret
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000395 ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
396 ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
397
Soby Mathewa43d4312014-04-07 15:28:55 +0100398restore_gp_registers_callee_eret:
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000399 ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
400 ldp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
401 ldp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
402 ldp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
403 ldp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
404 ldp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
Soby Mathewc3260f92014-04-30 15:36:37 +0100405 ldp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
406 ldp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
407 ldp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
408 ldp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
409 ldp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
410 ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
Soby Mathewa43d4312014-04-07 15:28:55 +0100411 ldp x30, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
412 msr sp_el0, x17
413 ldp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
414 eret