blob: 0baa9b2f7c96f6f2c92c0b21b1d8a22f0a9a9b75 [file] [log] [blame]
Achin Gupta9ac63c52014-01-16 12:08:03 +00001/*
Yatharth Kochar6c0566c2015-10-02 17:56:48 +01002 * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
Achin Gupta9ac63c52014-01-16 12:08:03 +00003 *
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
Dan Handley2bd4ef22014-04-09 13:14:54 +010031#include <arch.h>
Andrew Thoelke38bde412014-03-18 13:46:55 +000032#include <asm_macros.S>
Dan Handley2bd4ef22014-04-09 13:14:54 +010033#include <context.h>
Achin Gupta9ac63c52014-01-16 12:08:03 +000034
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010035 .global el1_sysregs_context_save
36 .global el1_sysregs_context_restore
37#if CTX_INCLUDE_FPREGS
38 .global fpregs_context_save
39 .global fpregs_context_restore
40#endif
41 .global save_gp_registers
42 .global restore_gp_registers_eret
43 .global restore_gp_registers_callee_eret
44 .global el3_exit
45
Achin Gupta9ac63c52014-01-16 12:08:03 +000046/* -----------------------------------------------------
47 * The following function strictly follows the AArch64
48 * PCS to use x9-x17 (temporary caller-saved registers)
Achin Gupta9ac63c52014-01-16 12:08:03 +000049 * to save EL1 system register context. It assumes that
50 * 'x0' is pointing to a 'el1_sys_regs' structure where
51 * the register context will be saved.
52 * -----------------------------------------------------
53 */
Andrew Thoelke38bde412014-03-18 13:46:55 +000054func el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +000055
56 mrs x9, spsr_el1
57 mrs x10, elr_el1
58 stp x9, x10, [x0, #CTX_SPSR_EL1]
59
60 mrs x11, spsr_abt
61 mrs x12, spsr_und
62 stp x11, x12, [x0, #CTX_SPSR_ABT]
63
64 mrs x13, spsr_irq
65 mrs x14, spsr_fiq
66 stp x13, x14, [x0, #CTX_SPSR_IRQ]
67
68 mrs x15, sctlr_el1
69 mrs x16, actlr_el1
70 stp x15, x16, [x0, #CTX_SCTLR_EL1]
71
72 mrs x17, cpacr_el1
73 mrs x9, csselr_el1
74 stp x17, x9, [x0, #CTX_CPACR_EL1]
75
76 mrs x10, sp_el1
77 mrs x11, esr_el1
78 stp x10, x11, [x0, #CTX_SP_EL1]
79
80 mrs x12, ttbr0_el1
81 mrs x13, ttbr1_el1
82 stp x12, x13, [x0, #CTX_TTBR0_EL1]
83
84 mrs x14, mair_el1
85 mrs x15, amair_el1
86 stp x14, x15, [x0, #CTX_MAIR_EL1]
87
88 mrs x16, tcr_el1
89 mrs x17, tpidr_el1
90 stp x16, x17, [x0, #CTX_TCR_EL1]
91
92 mrs x9, tpidr_el0
93 mrs x10, tpidrro_el0
94 stp x9, x10, [x0, #CTX_TPIDR_EL0]
95
96 mrs x11, dacr32_el2
97 mrs x12, ifsr32_el2
98 stp x11, x12, [x0, #CTX_DACR32_EL2]
99
100 mrs x13, par_el1
101 mrs x14, far_el1
102 stp x13, x14, [x0, #CTX_PAR_EL1]
103
104 mrs x15, afsr0_el1
105 mrs x16, afsr1_el1
106 stp x15, x16, [x0, #CTX_AFSR0_EL1]
107
108 mrs x17, contextidr_el1
109 mrs x9, vbar_el1
110 stp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
111
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100112 /* Save NS timer registers if the build has instructed so */
113#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000114 mrs x10, cntp_ctl_el0
115 mrs x11, cntp_cval_el0
116 stp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
117
118 mrs x12, cntv_ctl_el0
119 mrs x13, cntv_cval_el0
120 stp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
121
122 mrs x14, cntkctl_el1
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100123 str x14, [x0, #CTX_CNTKCTL_EL1]
124#endif
125
Achin Gupta9ac63c52014-01-16 12:08:03 +0000126 mrs x15, fpexc32_el2
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100127 str x15, [x0, #CTX_FP_FPEXC32_EL2]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000128
129 ret
Kévin Petita877c252015-03-24 14:03:57 +0000130endfunc el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000131
132/* -----------------------------------------------------
133 * The following function strictly follows the AArch64
134 * PCS to use x9-x17 (temporary caller-saved registers)
135 * to restore EL1 system register context. It assumes
136 * that 'x0' is pointing to a 'el1_sys_regs' structure
137 * from where the register context will be restored
138 * -----------------------------------------------------
139 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000140func el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000141
142 ldp x9, x10, [x0, #CTX_SPSR_EL1]
143 msr spsr_el1, x9
144 msr elr_el1, x10
145
146 ldp x11, x12, [x0, #CTX_SPSR_ABT]
147 msr spsr_abt, x11
148 msr spsr_und, x12
149
150 ldp x13, x14, [x0, #CTX_SPSR_IRQ]
151 msr spsr_irq, x13
152 msr spsr_fiq, x14
153
154 ldp x15, x16, [x0, #CTX_SCTLR_EL1]
155 msr sctlr_el1, x15
156 msr actlr_el1, x16
157
158 ldp x17, x9, [x0, #CTX_CPACR_EL1]
159 msr cpacr_el1, x17
160 msr csselr_el1, x9
161
162 ldp x10, x11, [x0, #CTX_SP_EL1]
163 msr sp_el1, x10
164 msr esr_el1, x11
165
166 ldp x12, x13, [x0, #CTX_TTBR0_EL1]
167 msr ttbr0_el1, x12
168 msr ttbr1_el1, x13
169
170 ldp x14, x15, [x0, #CTX_MAIR_EL1]
171 msr mair_el1, x14
172 msr amair_el1, x15
173
174 ldp x16, x17, [x0, #CTX_TCR_EL1]
175 msr tcr_el1, x16
176 msr tpidr_el1, x17
177
178 ldp x9, x10, [x0, #CTX_TPIDR_EL0]
179 msr tpidr_el0, x9
180 msr tpidrro_el0, x10
181
182 ldp x11, x12, [x0, #CTX_DACR32_EL2]
183 msr dacr32_el2, x11
184 msr ifsr32_el2, x12
185
186 ldp x13, x14, [x0, #CTX_PAR_EL1]
187 msr par_el1, x13
188 msr far_el1, x14
189
190 ldp x15, x16, [x0, #CTX_AFSR0_EL1]
191 msr afsr0_el1, x15
192 msr afsr1_el1, x16
193
194 ldp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
195 msr contextidr_el1, x17
196 msr vbar_el1, x9
197
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100198 /* Restore NS timer registers if the build has instructed so */
199#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000200 ldp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
201 msr cntp_ctl_el0, x10
202 msr cntp_cval_el0, x11
203
204 ldp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
205 msr cntv_ctl_el0, x12
206 msr cntv_cval_el0, x13
207
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100208 ldr x14, [x0, #CTX_CNTKCTL_EL1]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000209 msr cntkctl_el1, x14
Jeenu Viswambharand1b60152014-05-12 15:28:47 +0100210#endif
211
212 ldr x15, [x0, #CTX_FP_FPEXC32_EL2]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000213 msr fpexc32_el2, x15
214
215 /* No explict ISB required here as ERET covers it */
216
217 ret
Kévin Petita877c252015-03-24 14:03:57 +0000218endfunc el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000219
220/* -----------------------------------------------------
Sandrine Bailleux046cd3f2014-08-06 11:27:23 +0100221 * The following function follows the aapcs_64 strictly
Achin Gupta9ac63c52014-01-16 12:08:03 +0000222 * to use x9-x17 (temporary caller-saved registers
223 * according to AArch64 PCS) to save floating point
224 * register context. It assumes that 'x0' is pointing to
225 * a 'fp_regs' structure where the register context will
226 * be saved.
227 *
228 * Access to VFP registers will trap if CPTR_EL3.TFP is
229 * set. However currently we don't use VFP registers
230 * nor set traps in Trusted Firmware, and assume it's
231 * cleared
232 *
233 * TODO: Revisit when VFP is used in secure world
234 * -----------------------------------------------------
235 */
Juan Castillo258e94f2014-06-25 17:26:36 +0100236#if CTX_INCLUDE_FPREGS
Andrew Thoelke38bde412014-03-18 13:46:55 +0000237func fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000238 stp q0, q1, [x0, #CTX_FP_Q0]
239 stp q2, q3, [x0, #CTX_FP_Q2]
240 stp q4, q5, [x0, #CTX_FP_Q4]
241 stp q6, q7, [x0, #CTX_FP_Q6]
242 stp q8, q9, [x0, #CTX_FP_Q8]
243 stp q10, q11, [x0, #CTX_FP_Q10]
244 stp q12, q13, [x0, #CTX_FP_Q12]
245 stp q14, q15, [x0, #CTX_FP_Q14]
246 stp q16, q17, [x0, #CTX_FP_Q16]
247 stp q18, q19, [x0, #CTX_FP_Q18]
248 stp q20, q21, [x0, #CTX_FP_Q20]
249 stp q22, q23, [x0, #CTX_FP_Q22]
250 stp q24, q25, [x0, #CTX_FP_Q24]
251 stp q26, q27, [x0, #CTX_FP_Q26]
252 stp q28, q29, [x0, #CTX_FP_Q28]
253 stp q30, q31, [x0, #CTX_FP_Q30]
254
255 mrs x9, fpsr
256 str x9, [x0, #CTX_FP_FPSR]
257
258 mrs x10, fpcr
259 str x10, [x0, #CTX_FP_FPCR]
260
261 ret
Kévin Petita877c252015-03-24 14:03:57 +0000262endfunc fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000263
264/* -----------------------------------------------------
265 * The following function follows the aapcs_64 strictly
266 * to use x9-x17 (temporary caller-saved registers
267 * according to AArch64 PCS) to restore floating point
268 * register context. It assumes that 'x0' is pointing to
269 * a 'fp_regs' structure from where the register context
270 * will be restored.
271 *
272 * Access to VFP registers will trap if CPTR_EL3.TFP is
273 * set. However currently we don't use VFP registers
274 * nor set traps in Trusted Firmware, and assume it's
275 * cleared
276 *
277 * TODO: Revisit when VFP is used in secure world
278 * -----------------------------------------------------
279 */
Andrew Thoelke38bde412014-03-18 13:46:55 +0000280func fpregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000281 ldp q0, q1, [x0, #CTX_FP_Q0]
282 ldp q2, q3, [x0, #CTX_FP_Q2]
283 ldp q4, q5, [x0, #CTX_FP_Q4]
284 ldp q6, q7, [x0, #CTX_FP_Q6]
285 ldp q8, q9, [x0, #CTX_FP_Q8]
286 ldp q10, q11, [x0, #CTX_FP_Q10]
287 ldp q12, q13, [x0, #CTX_FP_Q12]
288 ldp q14, q15, [x0, #CTX_FP_Q14]
289 ldp q16, q17, [x0, #CTX_FP_Q16]
290 ldp q18, q19, [x0, #CTX_FP_Q18]
291 ldp q20, q21, [x0, #CTX_FP_Q20]
292 ldp q22, q23, [x0, #CTX_FP_Q22]
293 ldp q24, q25, [x0, #CTX_FP_Q24]
294 ldp q26, q27, [x0, #CTX_FP_Q26]
295 ldp q28, q29, [x0, #CTX_FP_Q28]
296 ldp q30, q31, [x0, #CTX_FP_Q30]
297
298 ldr x9, [x0, #CTX_FP_FPSR]
299 msr fpsr, x9
300
Soby Mathewe77e1162015-12-03 09:42:50 +0000301 ldr x10, [x0, #CTX_FP_FPCR]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000302 msr fpcr, x10
303
304 /*
305 * No explict ISB required here as ERET to
Sandrine Bailleuxf4119ec2015-12-17 13:58:58 +0000306 * switch to secure EL1 or non-secure world
Achin Gupta9ac63c52014-01-16 12:08:03 +0000307 * covers it
308 */
309
310 ret
Kévin Petita877c252015-03-24 14:03:57 +0000311endfunc fpregs_context_restore
Juan Castillo258e94f2014-06-25 17:26:36 +0100312#endif /* CTX_INCLUDE_FPREGS */
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100313
314/* -----------------------------------------------------
315 * The following functions are used to save and restore
316 * all the general purpose registers. Ideally we would
317 * only save and restore the callee saved registers when
318 * a world switch occurs but that type of implementation
319 * is more complex. So currently we will always save and
320 * restore these registers on entry and exit of EL3.
321 * These are not macros to ensure their invocation fits
322 * within the 32 instructions per exception vector.
323 * clobbers: x18
324 * -----------------------------------------------------
325 */
326func save_gp_registers
327 stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
328 stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
329 stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
330 stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
331 stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
332 stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
333 stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
334 stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
335 stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
336 stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
337 stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
338 stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
339 stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
340 stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
341 stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
342 mrs x18, sp_el0
343 str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
344 ret
345endfunc save_gp_registers
346
347func restore_gp_registers_eret
348 ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
349 ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
350 b restore_gp_registers_callee_eret
351endfunc restore_gp_registers_eret
352
353func restore_gp_registers_callee_eret
354 ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
355 ldp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
356 ldp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
357 ldp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
358 ldp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
359 ldp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
360 ldp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
361 ldp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
362 ldp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
363 ldp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
364 ldp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
365 ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
366 ldp x30, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
367 msr sp_el0, x17
368 ldp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
369 eret
370endfunc restore_gp_registers_callee_eret
371
372 /* -----------------------------------------------------
373 * This routine assumes that the SP_EL3 is pointing to
374 * a valid context structure from where the gp regs and
375 * other special registers can be retrieved.
376 * -----------------------------------------------------
377 */
378func el3_exit
379 /* -----------------------------------------------------
380 * Save the current SP_EL0 i.e. the EL3 runtime stack
381 * which will be used for handling the next SMC. Then
382 * switch to SP_EL3
383 * -----------------------------------------------------
384 */
385 mov x17, sp
386 msr spsel, #1
387 str x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
388
389 /* -----------------------------------------------------
390 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
391 * -----------------------------------------------------
392 */
393 ldr x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
394 ldp x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
395 msr scr_el3, x18
396 msr spsr_el3, x16
397 msr elr_el3, x17
398
399 /* Restore saved general purpose registers and return */
400 b restore_gp_registers_eret
401endfunc el3_exit