Achin Gupta | 9ac63c5 | 2014-01-16 12:08:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 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 Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 31 | #include <arch.h> |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 32 | #include <asm_macros.S> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <context.h> |
Achin Gupta | 9ac63c5 | 2014-01-16 12:08:03 +0000 | [diff] [blame] | 34 | |
| 35 | /* ----------------------------------------------------- |
| 36 | * The following function strictly follows the AArch64 |
| 37 | * PCS to use x9-x17 (temporary caller-saved registers) |
| 38 | * to save essential EL3 system register context. It |
| 39 | * assumes that 'x0' is pointing to a 'el1_sys_regs' |
| 40 | * structure where the register context will be saved. |
| 41 | * ----------------------------------------------------- |
| 42 | */ |
| 43 | .global el3_sysregs_context_save |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 44 | func el3_sysregs_context_save |
Achin Gupta | 9ac63c5 | 2014-01-16 12:08:03 +0000 | [diff] [blame] | 45 | |
| 46 | mrs x9, scr_el3 |
| 47 | mrs x10, sctlr_el3 |
| 48 | stp x9, x10, [x0, #CTX_SCR_EL3] |
| 49 | |
| 50 | mrs x11, cptr_el3 |
| 51 | stp x11, xzr, [x0, #CTX_CPTR_EL3] |
| 52 | |
| 53 | mrs x13, cntfrq_el0 |
| 54 | mrs x14, mair_el3 |
| 55 | stp x13, x14, [x0, #CTX_CNTFRQ_EL0] |
| 56 | |
| 57 | mrs x15, tcr_el3 |
| 58 | mrs x16, ttbr0_el3 |
| 59 | stp x15, x16, [x0, #CTX_TCR_EL3] |
| 60 | |
| 61 | mrs x17, daif |
| 62 | and x17, x17, #(DAIF_ABT_BIT | DAIF_DBG_BIT) |
| 63 | stp x17, xzr, [x0, #CTX_DAIF_EL3] |
| 64 | |
| 65 | ret |
| 66 | |
| 67 | /* ----------------------------------------------------- |
| 68 | * The following function strictly follows the AArch64 |
| 69 | * PCS to use x9-x17 (temporary caller-saved registers) |
| 70 | * to restore essential EL3 system register context. It |
| 71 | * assumes that 'x0' is pointing to a 'el1_sys_regs' |
| 72 | * structure from where the register context will be |
| 73 | * restored. |
| 74 | * |
| 75 | * Note that the sequence differs from that of the save |
| 76 | * function as we want the MMU to be enabled last |
| 77 | * ----------------------------------------------------- |
| 78 | */ |
| 79 | .global el3_sysregs_context_restore |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 80 | func el3_sysregs_context_restore |
Achin Gupta | 9ac63c5 | 2014-01-16 12:08:03 +0000 | [diff] [blame] | 81 | |
| 82 | ldp x11, xzr, [x0, #CTX_CPTR_EL3] |
| 83 | msr cptr_el3, x11 |
| 84 | |
| 85 | ldp x13, x14, [x0, #CTX_CNTFRQ_EL0] |
| 86 | msr cntfrq_el0, x13 |
| 87 | msr mair_el3, x14 |
| 88 | |
| 89 | ldp x15, x16, [x0, #CTX_TCR_EL3] |
| 90 | msr tcr_el3, x15 |
| 91 | msr ttbr0_el3, x16 |
| 92 | |
| 93 | ldp x17, xzr, [x0, #CTX_DAIF_EL3] |
| 94 | mrs x11, daif |
| 95 | orr x17, x17, x11 |
| 96 | msr daif, x17 |
| 97 | |
| 98 | /* Make sure all the above changes are observed */ |
| 99 | isb |
| 100 | |
| 101 | ldp x9, x10, [x0, #CTX_SCR_EL3] |
| 102 | msr scr_el3, x9 |
| 103 | msr sctlr_el3, x10 |
| 104 | isb |
| 105 | |
| 106 | ret |
| 107 | |
| 108 | /* ----------------------------------------------------- |
| 109 | * The following function strictly follows the AArch64 |
| 110 | * PCS to use x9-x17 (temporary caller-saved registers) |
| 111 | * to save EL1 system register context. It assumes that |
| 112 | * 'x0' is pointing to a 'el1_sys_regs' structure where |
| 113 | * the register context will be saved. |
| 114 | * ----------------------------------------------------- |
| 115 | */ |
| 116 | .global el1_sysregs_context_save |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 117 | func el1_sysregs_context_save |
Achin Gupta | 9ac63c5 | 2014-01-16 12:08:03 +0000 | [diff] [blame] | 118 | |
| 119 | mrs x9, spsr_el1 |
| 120 | mrs x10, elr_el1 |
| 121 | stp x9, x10, [x0, #CTX_SPSR_EL1] |
| 122 | |
| 123 | mrs x11, spsr_abt |
| 124 | mrs x12, spsr_und |
| 125 | stp x11, x12, [x0, #CTX_SPSR_ABT] |
| 126 | |
| 127 | mrs x13, spsr_irq |
| 128 | mrs x14, spsr_fiq |
| 129 | stp x13, x14, [x0, #CTX_SPSR_IRQ] |
| 130 | |
| 131 | mrs x15, sctlr_el1 |
| 132 | mrs x16, actlr_el1 |
| 133 | stp x15, x16, [x0, #CTX_SCTLR_EL1] |
| 134 | |
| 135 | mrs x17, cpacr_el1 |
| 136 | mrs x9, csselr_el1 |
| 137 | stp x17, x9, [x0, #CTX_CPACR_EL1] |
| 138 | |
| 139 | mrs x10, sp_el1 |
| 140 | mrs x11, esr_el1 |
| 141 | stp x10, x11, [x0, #CTX_SP_EL1] |
| 142 | |
| 143 | mrs x12, ttbr0_el1 |
| 144 | mrs x13, ttbr1_el1 |
| 145 | stp x12, x13, [x0, #CTX_TTBR0_EL1] |
| 146 | |
| 147 | mrs x14, mair_el1 |
| 148 | mrs x15, amair_el1 |
| 149 | stp x14, x15, [x0, #CTX_MAIR_EL1] |
| 150 | |
| 151 | mrs x16, tcr_el1 |
| 152 | mrs x17, tpidr_el1 |
| 153 | stp x16, x17, [x0, #CTX_TCR_EL1] |
| 154 | |
| 155 | mrs x9, tpidr_el0 |
| 156 | mrs x10, tpidrro_el0 |
| 157 | stp x9, x10, [x0, #CTX_TPIDR_EL0] |
| 158 | |
| 159 | mrs x11, dacr32_el2 |
| 160 | mrs x12, ifsr32_el2 |
| 161 | stp x11, x12, [x0, #CTX_DACR32_EL2] |
| 162 | |
| 163 | mrs x13, par_el1 |
| 164 | mrs x14, far_el1 |
| 165 | stp x13, x14, [x0, #CTX_PAR_EL1] |
| 166 | |
| 167 | mrs x15, afsr0_el1 |
| 168 | mrs x16, afsr1_el1 |
| 169 | stp x15, x16, [x0, #CTX_AFSR0_EL1] |
| 170 | |
| 171 | mrs x17, contextidr_el1 |
| 172 | mrs x9, vbar_el1 |
| 173 | stp x17, x9, [x0, #CTX_CONTEXTIDR_EL1] |
| 174 | |
| 175 | mrs x10, cntp_ctl_el0 |
| 176 | mrs x11, cntp_cval_el0 |
| 177 | stp x10, x11, [x0, #CTX_CNTP_CTL_EL0] |
| 178 | |
| 179 | mrs x12, cntv_ctl_el0 |
| 180 | mrs x13, cntv_cval_el0 |
| 181 | stp x12, x13, [x0, #CTX_CNTV_CTL_EL0] |
| 182 | |
| 183 | mrs x14, cntkctl_el1 |
| 184 | mrs x15, fpexc32_el2 |
| 185 | stp x14, x15, [x0, #CTX_CNTKCTL_EL1] |
| 186 | |
| 187 | ret |
| 188 | |
| 189 | /* ----------------------------------------------------- |
| 190 | * The following function strictly follows the AArch64 |
| 191 | * PCS to use x9-x17 (temporary caller-saved registers) |
| 192 | * to restore EL1 system register context. It assumes |
| 193 | * that 'x0' is pointing to a 'el1_sys_regs' structure |
| 194 | * from where the register context will be restored |
| 195 | * ----------------------------------------------------- |
| 196 | */ |
| 197 | .global el1_sysregs_context_restore |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 198 | func el1_sysregs_context_restore |
Achin Gupta | 9ac63c5 | 2014-01-16 12:08:03 +0000 | [diff] [blame] | 199 | |
| 200 | ldp x9, x10, [x0, #CTX_SPSR_EL1] |
| 201 | msr spsr_el1, x9 |
| 202 | msr elr_el1, x10 |
| 203 | |
| 204 | ldp x11, x12, [x0, #CTX_SPSR_ABT] |
| 205 | msr spsr_abt, x11 |
| 206 | msr spsr_und, x12 |
| 207 | |
| 208 | ldp x13, x14, [x0, #CTX_SPSR_IRQ] |
| 209 | msr spsr_irq, x13 |
| 210 | msr spsr_fiq, x14 |
| 211 | |
| 212 | ldp x15, x16, [x0, #CTX_SCTLR_EL1] |
| 213 | msr sctlr_el1, x15 |
| 214 | msr actlr_el1, x16 |
| 215 | |
| 216 | ldp x17, x9, [x0, #CTX_CPACR_EL1] |
| 217 | msr cpacr_el1, x17 |
| 218 | msr csselr_el1, x9 |
| 219 | |
| 220 | ldp x10, x11, [x0, #CTX_SP_EL1] |
| 221 | msr sp_el1, x10 |
| 222 | msr esr_el1, x11 |
| 223 | |
| 224 | ldp x12, x13, [x0, #CTX_TTBR0_EL1] |
| 225 | msr ttbr0_el1, x12 |
| 226 | msr ttbr1_el1, x13 |
| 227 | |
| 228 | ldp x14, x15, [x0, #CTX_MAIR_EL1] |
| 229 | msr mair_el1, x14 |
| 230 | msr amair_el1, x15 |
| 231 | |
| 232 | ldp x16, x17, [x0, #CTX_TCR_EL1] |
| 233 | msr tcr_el1, x16 |
| 234 | msr tpidr_el1, x17 |
| 235 | |
| 236 | ldp x9, x10, [x0, #CTX_TPIDR_EL0] |
| 237 | msr tpidr_el0, x9 |
| 238 | msr tpidrro_el0, x10 |
| 239 | |
| 240 | ldp x11, x12, [x0, #CTX_DACR32_EL2] |
| 241 | msr dacr32_el2, x11 |
| 242 | msr ifsr32_el2, x12 |
| 243 | |
| 244 | ldp x13, x14, [x0, #CTX_PAR_EL1] |
| 245 | msr par_el1, x13 |
| 246 | msr far_el1, x14 |
| 247 | |
| 248 | ldp x15, x16, [x0, #CTX_AFSR0_EL1] |
| 249 | msr afsr0_el1, x15 |
| 250 | msr afsr1_el1, x16 |
| 251 | |
| 252 | ldp x17, x9, [x0, #CTX_CONTEXTIDR_EL1] |
| 253 | msr contextidr_el1, x17 |
| 254 | msr vbar_el1, x9 |
| 255 | |
| 256 | ldp x10, x11, [x0, #CTX_CNTP_CTL_EL0] |
| 257 | msr cntp_ctl_el0, x10 |
| 258 | msr cntp_cval_el0, x11 |
| 259 | |
| 260 | ldp x12, x13, [x0, #CTX_CNTV_CTL_EL0] |
| 261 | msr cntv_ctl_el0, x12 |
| 262 | msr cntv_cval_el0, x13 |
| 263 | |
| 264 | ldp x14, x15, [x0, #CTX_CNTKCTL_EL1] |
| 265 | msr cntkctl_el1, x14 |
| 266 | msr fpexc32_el2, x15 |
| 267 | |
| 268 | /* No explict ISB required here as ERET covers it */ |
| 269 | |
| 270 | ret |
| 271 | |
| 272 | /* ----------------------------------------------------- |
| 273 | * The followsing function follows the aapcs_64 strictly |
| 274 | * to use x9-x17 (temporary caller-saved registers |
| 275 | * according to AArch64 PCS) to save floating point |
| 276 | * register context. It assumes that 'x0' is pointing to |
| 277 | * a 'fp_regs' structure where the register context will |
| 278 | * be saved. |
| 279 | * |
| 280 | * Access to VFP registers will trap if CPTR_EL3.TFP is |
| 281 | * set. However currently we don't use VFP registers |
| 282 | * nor set traps in Trusted Firmware, and assume it's |
| 283 | * cleared |
| 284 | * |
| 285 | * TODO: Revisit when VFP is used in secure world |
| 286 | * ----------------------------------------------------- |
| 287 | */ |
| 288 | .global fpregs_context_save |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 289 | func fpregs_context_save |
Achin Gupta | 9ac63c5 | 2014-01-16 12:08:03 +0000 | [diff] [blame] | 290 | stp q0, q1, [x0, #CTX_FP_Q0] |
| 291 | stp q2, q3, [x0, #CTX_FP_Q2] |
| 292 | stp q4, q5, [x0, #CTX_FP_Q4] |
| 293 | stp q6, q7, [x0, #CTX_FP_Q6] |
| 294 | stp q8, q9, [x0, #CTX_FP_Q8] |
| 295 | stp q10, q11, [x0, #CTX_FP_Q10] |
| 296 | stp q12, q13, [x0, #CTX_FP_Q12] |
| 297 | stp q14, q15, [x0, #CTX_FP_Q14] |
| 298 | stp q16, q17, [x0, #CTX_FP_Q16] |
| 299 | stp q18, q19, [x0, #CTX_FP_Q18] |
| 300 | stp q20, q21, [x0, #CTX_FP_Q20] |
| 301 | stp q22, q23, [x0, #CTX_FP_Q22] |
| 302 | stp q24, q25, [x0, #CTX_FP_Q24] |
| 303 | stp q26, q27, [x0, #CTX_FP_Q26] |
| 304 | stp q28, q29, [x0, #CTX_FP_Q28] |
| 305 | stp q30, q31, [x0, #CTX_FP_Q30] |
| 306 | |
| 307 | mrs x9, fpsr |
| 308 | str x9, [x0, #CTX_FP_FPSR] |
| 309 | |
| 310 | mrs x10, fpcr |
| 311 | str x10, [x0, #CTX_FP_FPCR] |
| 312 | |
| 313 | ret |
| 314 | |
| 315 | /* ----------------------------------------------------- |
| 316 | * The following function follows the aapcs_64 strictly |
| 317 | * to use x9-x17 (temporary caller-saved registers |
| 318 | * according to AArch64 PCS) to restore floating point |
| 319 | * register context. It assumes that 'x0' is pointing to |
| 320 | * a 'fp_regs' structure from where the register context |
| 321 | * will be restored. |
| 322 | * |
| 323 | * Access to VFP registers will trap if CPTR_EL3.TFP is |
| 324 | * set. However currently we don't use VFP registers |
| 325 | * nor set traps in Trusted Firmware, and assume it's |
| 326 | * cleared |
| 327 | * |
| 328 | * TODO: Revisit when VFP is used in secure world |
| 329 | * ----------------------------------------------------- |
| 330 | */ |
| 331 | .global fpregs_context_restore |
Andrew Thoelke | 38bde41 | 2014-03-18 13:46:55 +0000 | [diff] [blame] | 332 | func fpregs_context_restore |
Achin Gupta | 9ac63c5 | 2014-01-16 12:08:03 +0000 | [diff] [blame] | 333 | ldp q0, q1, [x0, #CTX_FP_Q0] |
| 334 | ldp q2, q3, [x0, #CTX_FP_Q2] |
| 335 | ldp q4, q5, [x0, #CTX_FP_Q4] |
| 336 | ldp q6, q7, [x0, #CTX_FP_Q6] |
| 337 | ldp q8, q9, [x0, #CTX_FP_Q8] |
| 338 | ldp q10, q11, [x0, #CTX_FP_Q10] |
| 339 | ldp q12, q13, [x0, #CTX_FP_Q12] |
| 340 | ldp q14, q15, [x0, #CTX_FP_Q14] |
| 341 | ldp q16, q17, [x0, #CTX_FP_Q16] |
| 342 | ldp q18, q19, [x0, #CTX_FP_Q18] |
| 343 | ldp q20, q21, [x0, #CTX_FP_Q20] |
| 344 | ldp q22, q23, [x0, #CTX_FP_Q22] |
| 345 | ldp q24, q25, [x0, #CTX_FP_Q24] |
| 346 | ldp q26, q27, [x0, #CTX_FP_Q26] |
| 347 | ldp q28, q29, [x0, #CTX_FP_Q28] |
| 348 | ldp q30, q31, [x0, #CTX_FP_Q30] |
| 349 | |
| 350 | ldr x9, [x0, #CTX_FP_FPSR] |
| 351 | msr fpsr, x9 |
| 352 | |
| 353 | str x10, [x0, #CTX_FP_FPCR] |
| 354 | msr fpcr, x10 |
| 355 | |
| 356 | /* |
| 357 | * No explict ISB required here as ERET to |
| 358 | * swtich to secure EL1 or non-secure world |
| 359 | * covers it |
| 360 | */ |
| 361 | |
| 362 | ret |