1532ed618SSoby Mathew/* 230788a84SGovindraj Raja * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5532ed618SSoby Mathew */ 6532ed618SSoby Mathew 7532ed618SSoby Mathew#include <arch.h> 8532ed618SSoby Mathew#include <asm_macros.S> 9bb9549baSJan Dabros#include <assert_macros.S> 10532ed618SSoby Mathew#include <context.h> 113b8456bdSManish V Badarkhe#include <el3_common_macros.S> 12532ed618SSoby Mathew 13532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS 14532ed618SSoby Mathew .global fpregs_context_save 15532ed618SSoby Mathew .global fpregs_context_restore 160ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_FPREGS */ 1797215e0fSDaniel Boulby .global prepare_el3_entry 18ed108b56SAlexei Fedorov .global restore_gp_pmcr_pauth_regs 193b8456bdSManish V Badarkhe .global save_and_update_ptw_el1_sys_regs 20532ed618SSoby Mathew .global el3_exit 21532ed618SSoby Mathew 22ed108b56SAlexei Fedorov/* ------------------------------------------------------------------ 23ed108b56SAlexei Fedorov * The following function follows the aapcs_64 strictly to use 24ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers according to AArch64 PCS) 25ed108b56SAlexei Fedorov * to save floating point register context. It assumes that 'x0' is 26ed108b56SAlexei Fedorov * pointing to a 'fp_regs' structure where the register context will 27532ed618SSoby Mathew * be saved. 28532ed618SSoby Mathew * 29ed108b56SAlexei Fedorov * Access to VFP registers will trap if CPTR_EL3.TFP is set. 30ed108b56SAlexei Fedorov * However currently we don't use VFP registers nor set traps in 31ed108b56SAlexei Fedorov * Trusted Firmware, and assume it's cleared. 32532ed618SSoby Mathew * 33532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world 34ed108b56SAlexei Fedorov * ------------------------------------------------------------------ 35532ed618SSoby Mathew */ 36532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS 37532ed618SSoby Mathewfunc fpregs_context_save 38532ed618SSoby Mathew stp q0, q1, [x0, #CTX_FP_Q0] 39532ed618SSoby Mathew stp q2, q3, [x0, #CTX_FP_Q2] 40532ed618SSoby Mathew stp q4, q5, [x0, #CTX_FP_Q4] 41532ed618SSoby Mathew stp q6, q7, [x0, #CTX_FP_Q6] 42532ed618SSoby Mathew stp q8, q9, [x0, #CTX_FP_Q8] 43532ed618SSoby Mathew stp q10, q11, [x0, #CTX_FP_Q10] 44532ed618SSoby Mathew stp q12, q13, [x0, #CTX_FP_Q12] 45532ed618SSoby Mathew stp q14, q15, [x0, #CTX_FP_Q14] 46532ed618SSoby Mathew stp q16, q17, [x0, #CTX_FP_Q16] 47532ed618SSoby Mathew stp q18, q19, [x0, #CTX_FP_Q18] 48532ed618SSoby Mathew stp q20, q21, [x0, #CTX_FP_Q20] 49532ed618SSoby Mathew stp q22, q23, [x0, #CTX_FP_Q22] 50532ed618SSoby Mathew stp q24, q25, [x0, #CTX_FP_Q24] 51532ed618SSoby Mathew stp q26, q27, [x0, #CTX_FP_Q26] 52532ed618SSoby Mathew stp q28, q29, [x0, #CTX_FP_Q28] 53532ed618SSoby Mathew stp q30, q31, [x0, #CTX_FP_Q30] 54532ed618SSoby Mathew 55532ed618SSoby Mathew mrs x9, fpsr 56532ed618SSoby Mathew str x9, [x0, #CTX_FP_FPSR] 57532ed618SSoby Mathew 58532ed618SSoby Mathew mrs x10, fpcr 59532ed618SSoby Mathew str x10, [x0, #CTX_FP_FPCR] 60532ed618SSoby Mathew 6191089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS 6291089f36SDavid Cunado mrs x11, fpexc32_el2 6391089f36SDavid Cunado str x11, [x0, #CTX_FP_FPEXC32_EL2] 640ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_AARCH32_REGS */ 65532ed618SSoby Mathew ret 66532ed618SSoby Mathewendfunc fpregs_context_save 67532ed618SSoby Mathew 68ed108b56SAlexei Fedorov/* ------------------------------------------------------------------ 69ed108b56SAlexei Fedorov * The following function follows the aapcs_64 strictly to use x9-x17 70ed108b56SAlexei Fedorov * (temporary caller-saved registers according to AArch64 PCS) to 71ed108b56SAlexei Fedorov * restore floating point register context. It assumes that 'x0' is 72ed108b56SAlexei Fedorov * pointing to a 'fp_regs' structure from where the register context 73532ed618SSoby Mathew * will be restored. 74532ed618SSoby Mathew * 75ed108b56SAlexei Fedorov * Access to VFP registers will trap if CPTR_EL3.TFP is set. 76ed108b56SAlexei Fedorov * However currently we don't use VFP registers nor set traps in 77ed108b56SAlexei Fedorov * Trusted Firmware, and assume it's cleared. 78532ed618SSoby Mathew * 79532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world 80ed108b56SAlexei Fedorov * ------------------------------------------------------------------ 81532ed618SSoby Mathew */ 82532ed618SSoby Mathewfunc fpregs_context_restore 83532ed618SSoby Mathew ldp q0, q1, [x0, #CTX_FP_Q0] 84532ed618SSoby Mathew ldp q2, q3, [x0, #CTX_FP_Q2] 85532ed618SSoby Mathew ldp q4, q5, [x0, #CTX_FP_Q4] 86532ed618SSoby Mathew ldp q6, q7, [x0, #CTX_FP_Q6] 87532ed618SSoby Mathew ldp q8, q9, [x0, #CTX_FP_Q8] 88532ed618SSoby Mathew ldp q10, q11, [x0, #CTX_FP_Q10] 89532ed618SSoby Mathew ldp q12, q13, [x0, #CTX_FP_Q12] 90532ed618SSoby Mathew ldp q14, q15, [x0, #CTX_FP_Q14] 91532ed618SSoby Mathew ldp q16, q17, [x0, #CTX_FP_Q16] 92532ed618SSoby Mathew ldp q18, q19, [x0, #CTX_FP_Q18] 93532ed618SSoby Mathew ldp q20, q21, [x0, #CTX_FP_Q20] 94532ed618SSoby Mathew ldp q22, q23, [x0, #CTX_FP_Q22] 95532ed618SSoby Mathew ldp q24, q25, [x0, #CTX_FP_Q24] 96532ed618SSoby Mathew ldp q26, q27, [x0, #CTX_FP_Q26] 97532ed618SSoby Mathew ldp q28, q29, [x0, #CTX_FP_Q28] 98532ed618SSoby Mathew ldp q30, q31, [x0, #CTX_FP_Q30] 99532ed618SSoby Mathew 100532ed618SSoby Mathew ldr x9, [x0, #CTX_FP_FPSR] 101532ed618SSoby Mathew msr fpsr, x9 102532ed618SSoby Mathew 103532ed618SSoby Mathew ldr x10, [x0, #CTX_FP_FPCR] 104532ed618SSoby Mathew msr fpcr, x10 105532ed618SSoby Mathew 10691089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS 10791089f36SDavid Cunado ldr x11, [x0, #CTX_FP_FPEXC32_EL2] 10891089f36SDavid Cunado msr fpexc32_el2, x11 1090ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_AARCH32_REGS */ 1100ce220afSJayanth Dodderi Chidanand 111532ed618SSoby Mathew /* 112532ed618SSoby Mathew * No explict ISB required here as ERET to 113532ed618SSoby Mathew * switch to secure EL1 or non-secure world 114532ed618SSoby Mathew * covers it 115532ed618SSoby Mathew */ 116532ed618SSoby Mathew 117532ed618SSoby Mathew ret 118532ed618SSoby Mathewendfunc fpregs_context_restore 119532ed618SSoby Mathew#endif /* CTX_INCLUDE_FPREGS */ 120532ed618SSoby Mathew 1217d33ffe4SDaniel Boulby /* 1221cbe42a5SManish Pandey * Set SCR_EL3.EA bit to enable SErrors at EL3 1231cbe42a5SManish Pandey */ 1241cbe42a5SManish Pandey .macro enable_serror_at_el3 1251cbe42a5SManish Pandey mrs x8, scr_el3 1261cbe42a5SManish Pandey orr x8, x8, #SCR_EA_BIT 1271cbe42a5SManish Pandey msr scr_el3, x8 1281cbe42a5SManish Pandey .endm 1291cbe42a5SManish Pandey 1301cbe42a5SManish Pandey /* 1317d33ffe4SDaniel Boulby * Set the PSTATE bits not set when the exception was taken as 1327d33ffe4SDaniel Boulby * described in the AArch64.TakeException() pseudocode function 1337d33ffe4SDaniel Boulby * in ARM DDI 0487F.c page J1-7635 to a default value. 1347d33ffe4SDaniel Boulby */ 1357d33ffe4SDaniel Boulby .macro set_unset_pstate_bits 1367d33ffe4SDaniel Boulby /* 1377d33ffe4SDaniel Boulby * If Data Independent Timing (DIT) functionality is implemented, 1387d33ffe4SDaniel Boulby * always enable DIT in EL3 1397d33ffe4SDaniel Boulby */ 1407d33ffe4SDaniel Boulby#if ENABLE_FEAT_DIT 14188727fc3SAndre Przywara#if ENABLE_FEAT_DIT == 2 14288727fc3SAndre Przywara mrs x8, id_aa64pfr0_el1 14388727fc3SAndre Przywara and x8, x8, #(ID_AA64PFR0_DIT_MASK << ID_AA64PFR0_DIT_SHIFT) 14488727fc3SAndre Przywara cbz x8, 1f 14588727fc3SAndre Przywara#endif 1467d33ffe4SDaniel Boulby mov x8, #DIT_BIT 1477d33ffe4SDaniel Boulby msr DIT, x8 14888727fc3SAndre Przywara1: 1497d33ffe4SDaniel Boulby#endif /* ENABLE_FEAT_DIT */ 1507d33ffe4SDaniel Boulby .endm /* set_unset_pstate_bits */ 1517d33ffe4SDaniel Boulby 152edebefbcSArvind Ram Prakash/*------------------------------------------------------------------------- 153edebefbcSArvind Ram Prakash * This macro checks the ENABLE_FEAT_MPAM state, performs ID register 154edebefbcSArvind Ram Prakash * check to see if the platform supports MPAM extension and restores MPAM3 155edebefbcSArvind Ram Prakash * register value if it is FEAT_STATE_ENABLED/FEAT_STATE_CHECKED. 156edebefbcSArvind Ram Prakash * 157edebefbcSArvind Ram Prakash * This is particularly more complicated because we can't check 158edebefbcSArvind Ram Prakash * if the platform supports MPAM by looking for status of a particular bit 159edebefbcSArvind Ram Prakash * in the MDCR_EL3 or CPTR_EL3 register like other extensions. 160edebefbcSArvind Ram Prakash * ------------------------------------------------------------------------ 161edebefbcSArvind Ram Prakash */ 162edebefbcSArvind Ram Prakash 163edebefbcSArvind Ram Prakash .macro restore_mpam3_el3 164edebefbcSArvind Ram Prakash#if ENABLE_FEAT_MPAM 165edebefbcSArvind Ram Prakash#if ENABLE_FEAT_MPAM == 2 166edebefbcSArvind Ram Prakash 167edebefbcSArvind Ram Prakash mrs x8, id_aa64pfr0_el1 168edebefbcSArvind Ram Prakash lsr x8, x8, #(ID_AA64PFR0_MPAM_SHIFT) 169edebefbcSArvind Ram Prakash and x8, x8, #(ID_AA64PFR0_MPAM_MASK) 170edebefbcSArvind Ram Prakash mrs x7, id_aa64pfr1_el1 171edebefbcSArvind Ram Prakash lsr x7, x7, #(ID_AA64PFR1_MPAM_FRAC_SHIFT) 172edebefbcSArvind Ram Prakash and x7, x7, #(ID_AA64PFR1_MPAM_FRAC_MASK) 173edebefbcSArvind Ram Prakash orr x7, x7, x8 174edebefbcSArvind Ram Prakash cbz x7, no_mpam 175edebefbcSArvind Ram Prakash#endif 176edebefbcSArvind Ram Prakash /* ----------------------------------------------------------- 177edebefbcSArvind Ram Prakash * Restore MPAM3_EL3 register as per context state 178edebefbcSArvind Ram Prakash * Currently we only enable MPAM for NS world and trap to EL3 179edebefbcSArvind Ram Prakash * for MPAM access in lower ELs of Secure and Realm world 180ac4f6aafSArvind Ram Prakash * x9 holds address of the per_world context 181edebefbcSArvind Ram Prakash * ----------------------------------------------------------- 182edebefbcSArvind Ram Prakash */ 183ac4f6aafSArvind Ram Prakash 184ac4f6aafSArvind Ram Prakash ldr x17, [x9, #CTX_MPAM3_EL3] 185edebefbcSArvind Ram Prakash msr S3_6_C10_C5_0, x17 /* mpam3_el3 */ 186edebefbcSArvind Ram Prakash 187edebefbcSArvind Ram Prakashno_mpam: 188edebefbcSArvind Ram Prakash#endif 189edebefbcSArvind Ram Prakash .endm /* restore_mpam3_el3 */ 190edebefbcSArvind Ram Prakash 191ed108b56SAlexei Fedorov/* ------------------------------------------------------------------ 19297215e0fSDaniel Boulby * The following macro is used to save and restore all the general 193ed108b56SAlexei Fedorov * purpose and ARMv8.3-PAuth (if enabled) registers. 194d64bfef5SJayanth Dodderi Chidanand * It also checks if the Secure Cycle Counter (PMCCNTR_EL0) 195d64bfef5SJayanth Dodderi Chidanand * is disabled in EL3/Secure (ARMv8.5-PMU), wherein PMCCNTR_EL0 196d64bfef5SJayanth Dodderi Chidanand * needs not to be saved/restored during world switch. 197ed108b56SAlexei Fedorov * 198ed108b56SAlexei Fedorov * Ideally we would only save and restore the callee saved registers 199ed108b56SAlexei Fedorov * when a world switch occurs but that type of implementation is more 200ed108b56SAlexei Fedorov * complex. So currently we will always save and restore these 201ed108b56SAlexei Fedorov * registers on entry and exit of EL3. 202532ed618SSoby Mathew * clobbers: x18 203ed108b56SAlexei Fedorov * ------------------------------------------------------------------ 204532ed618SSoby Mathew */ 20597215e0fSDaniel Boulby .macro save_gp_pmcr_pauth_regs 206532ed618SSoby Mathew stp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 207532ed618SSoby Mathew stp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2] 208532ed618SSoby Mathew stp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] 209532ed618SSoby Mathew stp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6] 210532ed618SSoby Mathew stp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8] 211532ed618SSoby Mathew stp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10] 212532ed618SSoby Mathew stp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12] 213532ed618SSoby Mathew stp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14] 214532ed618SSoby Mathew stp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16] 215532ed618SSoby Mathew stp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18] 216532ed618SSoby Mathew stp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20] 217532ed618SSoby Mathew stp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22] 218532ed618SSoby Mathew stp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24] 219532ed618SSoby Mathew stp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26] 220532ed618SSoby Mathew stp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 221532ed618SSoby Mathew mrs x18, sp_el0 222532ed618SSoby Mathew str x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0] 223c73686a1SBoyan Karatotev 224c73686a1SBoyan Karatotev /* PMUv3 is presumed to be always present */ 225ed108b56SAlexei Fedorov mrs x9, pmcr_el0 226ed108b56SAlexei Fedorov str x9, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0] 227ed108b56SAlexei Fedorov /* Disable cycle counter when event counting is prohibited */ 2281d6d6802SBoyan Karatotev orr x9, x9, #PMCR_EL0_DP_BIT 229ed108b56SAlexei Fedorov msr pmcr_el0, x9 230ed108b56SAlexei Fedorov isb 231ed108b56SAlexei Fedorov#if CTX_INCLUDE_PAUTH_REGS 232ed108b56SAlexei Fedorov /* ---------------------------------------------------------- 233ed108b56SAlexei Fedorov * Save the ARMv8.3-PAuth keys as they are not banked 234ed108b56SAlexei Fedorov * by exception level 235ed108b56SAlexei Fedorov * ---------------------------------------------------------- 236ed108b56SAlexei Fedorov */ 237ed108b56SAlexei Fedorov add x19, sp, #CTX_PAUTH_REGS_OFFSET 238ed108b56SAlexei Fedorov 239ed108b56SAlexei Fedorov mrs x20, APIAKeyLo_EL1 /* x21:x20 = APIAKey */ 240ed108b56SAlexei Fedorov mrs x21, APIAKeyHi_EL1 241ed108b56SAlexei Fedorov mrs x22, APIBKeyLo_EL1 /* x23:x22 = APIBKey */ 242ed108b56SAlexei Fedorov mrs x23, APIBKeyHi_EL1 243ed108b56SAlexei Fedorov mrs x24, APDAKeyLo_EL1 /* x25:x24 = APDAKey */ 244ed108b56SAlexei Fedorov mrs x25, APDAKeyHi_EL1 245ed108b56SAlexei Fedorov mrs x26, APDBKeyLo_EL1 /* x27:x26 = APDBKey */ 246ed108b56SAlexei Fedorov mrs x27, APDBKeyHi_EL1 247ed108b56SAlexei Fedorov mrs x28, APGAKeyLo_EL1 /* x29:x28 = APGAKey */ 248ed108b56SAlexei Fedorov mrs x29, APGAKeyHi_EL1 249ed108b56SAlexei Fedorov 250ed108b56SAlexei Fedorov stp x20, x21, [x19, #CTX_PACIAKEY_LO] 251ed108b56SAlexei Fedorov stp x22, x23, [x19, #CTX_PACIBKEY_LO] 252ed108b56SAlexei Fedorov stp x24, x25, [x19, #CTX_PACDAKEY_LO] 253ed108b56SAlexei Fedorov stp x26, x27, [x19, #CTX_PACDBKEY_LO] 254ed108b56SAlexei Fedorov stp x28, x29, [x19, #CTX_PACGAKEY_LO] 255ed108b56SAlexei Fedorov#endif /* CTX_INCLUDE_PAUTH_REGS */ 25697215e0fSDaniel Boulby .endm /* save_gp_pmcr_pauth_regs */ 25797215e0fSDaniel Boulby 25897215e0fSDaniel Boulby/* ----------------------------------------------------------------- 2597d33ffe4SDaniel Boulby * This function saves the context and sets the PSTATE to a known 2607d33ffe4SDaniel Boulby * state, preparing entry to el3. 26197215e0fSDaniel Boulby * Save all the general purpose and ARMv8.3-PAuth (if enabled) 26297215e0fSDaniel Boulby * registers. 2637d33ffe4SDaniel Boulby * Then set any of the PSTATE bits that are not set by hardware 2647d33ffe4SDaniel Boulby * according to the Aarch64.TakeException pseudocode in the Arm 2657d33ffe4SDaniel Boulby * Architecture Reference Manual to a default value for EL3. 2667d33ffe4SDaniel Boulby * clobbers: x17 26797215e0fSDaniel Boulby * ----------------------------------------------------------------- 26897215e0fSDaniel Boulby */ 26997215e0fSDaniel Boulbyfunc prepare_el3_entry 27097215e0fSDaniel Boulby save_gp_pmcr_pauth_regs 2711cbe42a5SManish Pandey enable_serror_at_el3 2727d33ffe4SDaniel Boulby /* 2737d33ffe4SDaniel Boulby * Set the PSTATE bits not described in the Aarch64.TakeException 2747d33ffe4SDaniel Boulby * pseudocode to their default values. 2757d33ffe4SDaniel Boulby */ 2767d33ffe4SDaniel Boulby set_unset_pstate_bits 277ed108b56SAlexei Fedorov ret 27897215e0fSDaniel Boulbyendfunc prepare_el3_entry 279ed108b56SAlexei Fedorov 280ed108b56SAlexei Fedorov/* ------------------------------------------------------------------ 281ed108b56SAlexei Fedorov * This function restores ARMv8.3-PAuth (if enabled) and all general 282ed108b56SAlexei Fedorov * purpose registers except x30 from the CPU context. 283ed108b56SAlexei Fedorov * x30 register must be explicitly restored by the caller. 284ed108b56SAlexei Fedorov * ------------------------------------------------------------------ 285ed108b56SAlexei Fedorov */ 286ed108b56SAlexei Fedorovfunc restore_gp_pmcr_pauth_regs 287ed108b56SAlexei Fedorov#if CTX_INCLUDE_PAUTH_REGS 288ed108b56SAlexei Fedorov /* Restore the ARMv8.3 PAuth keys */ 289ed108b56SAlexei Fedorov add x10, sp, #CTX_PAUTH_REGS_OFFSET 290ed108b56SAlexei Fedorov 291ed108b56SAlexei Fedorov ldp x0, x1, [x10, #CTX_PACIAKEY_LO] /* x1:x0 = APIAKey */ 292ed108b56SAlexei Fedorov ldp x2, x3, [x10, #CTX_PACIBKEY_LO] /* x3:x2 = APIBKey */ 293ed108b56SAlexei Fedorov ldp x4, x5, [x10, #CTX_PACDAKEY_LO] /* x5:x4 = APDAKey */ 294ed108b56SAlexei Fedorov ldp x6, x7, [x10, #CTX_PACDBKEY_LO] /* x7:x6 = APDBKey */ 295ed108b56SAlexei Fedorov ldp x8, x9, [x10, #CTX_PACGAKEY_LO] /* x9:x8 = APGAKey */ 296ed108b56SAlexei Fedorov 297ed108b56SAlexei Fedorov msr APIAKeyLo_EL1, x0 298ed108b56SAlexei Fedorov msr APIAKeyHi_EL1, x1 299ed108b56SAlexei Fedorov msr APIBKeyLo_EL1, x2 300ed108b56SAlexei Fedorov msr APIBKeyHi_EL1, x3 301ed108b56SAlexei Fedorov msr APDAKeyLo_EL1, x4 302ed108b56SAlexei Fedorov msr APDAKeyHi_EL1, x5 303ed108b56SAlexei Fedorov msr APDBKeyLo_EL1, x6 304ed108b56SAlexei Fedorov msr APDBKeyHi_EL1, x7 305ed108b56SAlexei Fedorov msr APGAKeyLo_EL1, x8 306ed108b56SAlexei Fedorov msr APGAKeyHi_EL1, x9 307ed108b56SAlexei Fedorov#endif /* CTX_INCLUDE_PAUTH_REGS */ 308c73686a1SBoyan Karatotev 309c73686a1SBoyan Karatotev /* PMUv3 is presumed to be always present */ 310ed108b56SAlexei Fedorov ldr x0, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0] 311ed108b56SAlexei Fedorov msr pmcr_el0, x0 312532ed618SSoby Mathew ldp x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0] 313532ed618SSoby Mathew ldp x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2] 314532ed618SSoby Mathew ldp x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4] 315532ed618SSoby Mathew ldp x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6] 316532ed618SSoby Mathew ldp x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8] 317532ed618SSoby Mathew ldp x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10] 318532ed618SSoby Mathew ldp x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12] 319532ed618SSoby Mathew ldp x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14] 320ef653d93SJeenu Viswambharan ldp x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16] 321532ed618SSoby Mathew ldp x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18] 322532ed618SSoby Mathew ldp x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20] 323532ed618SSoby Mathew ldp x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22] 324532ed618SSoby Mathew ldp x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24] 325532ed618SSoby Mathew ldp x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26] 326ef653d93SJeenu Viswambharan ldr x28, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0] 327ef653d93SJeenu Viswambharan msr sp_el0, x28 328532ed618SSoby Mathew ldp x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28] 329ef653d93SJeenu Viswambharan ret 330ed108b56SAlexei Fedorovendfunc restore_gp_pmcr_pauth_regs 331ef653d93SJeenu Viswambharan 3323b8456bdSManish V Badarkhe/* 3333b8456bdSManish V Badarkhe * In case of ERRATA_SPECULATIVE_AT, save SCTLR_EL1 and TCR_EL1 3343b8456bdSManish V Badarkhe * registers and update EL1 registers to disable stage1 and stage2 3353b8456bdSManish V Badarkhe * page table walk 3363b8456bdSManish V Badarkhe */ 3373b8456bdSManish V Badarkhefunc save_and_update_ptw_el1_sys_regs 3383b8456bdSManish V Badarkhe /* ---------------------------------------------------------- 3393b8456bdSManish V Badarkhe * Save only sctlr_el1 and tcr_el1 registers 3403b8456bdSManish V Badarkhe * ---------------------------------------------------------- 3413b8456bdSManish V Badarkhe */ 3423b8456bdSManish V Badarkhe mrs x29, sctlr_el1 3433b8456bdSManish V Badarkhe str x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1)] 3443b8456bdSManish V Badarkhe mrs x29, tcr_el1 3453b8456bdSManish V Badarkhe str x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_TCR_EL1)] 3463b8456bdSManish V Badarkhe 3473b8456bdSManish V Badarkhe /* ------------------------------------------------------------ 3483b8456bdSManish V Badarkhe * Must follow below order in order to disable page table 3493b8456bdSManish V Badarkhe * walk for lower ELs (EL1 and EL0). First step ensures that 3503b8456bdSManish V Badarkhe * page table walk is disabled for stage1 and second step 3513b8456bdSManish V Badarkhe * ensures that page table walker should use TCR_EL1.EPDx 3523b8456bdSManish V Badarkhe * bits to perform address translation. ISB ensures that CPU 3533b8456bdSManish V Badarkhe * does these 2 steps in order. 3543b8456bdSManish V Badarkhe * 3553b8456bdSManish V Badarkhe * 1. Update TCR_EL1.EPDx bits to disable page table walk by 3563b8456bdSManish V Badarkhe * stage1. 3573b8456bdSManish V Badarkhe * 2. Enable MMU bit to avoid identity mapping via stage2 3583b8456bdSManish V Badarkhe * and force TCR_EL1.EPDx to be used by the page table 3593b8456bdSManish V Badarkhe * walker. 3603b8456bdSManish V Badarkhe * ------------------------------------------------------------ 3613b8456bdSManish V Badarkhe */ 3623b8456bdSManish V Badarkhe orr x29, x29, #(TCR_EPD0_BIT) 3633b8456bdSManish V Badarkhe orr x29, x29, #(TCR_EPD1_BIT) 3643b8456bdSManish V Badarkhe msr tcr_el1, x29 3653b8456bdSManish V Badarkhe isb 3663b8456bdSManish V Badarkhe mrs x29, sctlr_el1 3673b8456bdSManish V Badarkhe orr x29, x29, #SCTLR_M_BIT 3683b8456bdSManish V Badarkhe msr sctlr_el1, x29 3693b8456bdSManish V Badarkhe isb 3703b8456bdSManish V Badarkhe 3713b8456bdSManish V Badarkhe ret 3723b8456bdSManish V Badarkheendfunc save_and_update_ptw_el1_sys_regs 3733b8456bdSManish V Badarkhe 374461c0a5dSElizabeth Ho/* ----------------------------------------------------------------- 375461c0a5dSElizabeth Ho* The below macro returns the address of the per_world context for 376461c0a5dSElizabeth Ho* the security state, retrieved through "get_security_state" macro. 377461c0a5dSElizabeth Ho* The per_world context address is returned in the register argument. 378461c0a5dSElizabeth Ho* Clobbers: x9, x10 379461c0a5dSElizabeth Ho* ------------------------------------------------------------------ 380461c0a5dSElizabeth Ho*/ 381461c0a5dSElizabeth Ho 382461c0a5dSElizabeth Ho.macro get_per_world_context _reg:req 383461c0a5dSElizabeth Ho ldr x10, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3] 384461c0a5dSElizabeth Ho get_security_state x9, x10 3854087ed6cSJayanth Dodderi Chidanand mov_imm x10, (CTX_PERWORLD_EL3STATE_END - CTX_CPTR_EL3) 386461c0a5dSElizabeth Ho mul x9, x9, x10 387461c0a5dSElizabeth Ho adrp x10, per_world_context 388461c0a5dSElizabeth Ho add x10, x10, :lo12:per_world_context 389461c0a5dSElizabeth Ho add x9, x9, x10 390461c0a5dSElizabeth Ho mov \_reg, x9 391461c0a5dSElizabeth Ho.endm 392461c0a5dSElizabeth Ho 393ed108b56SAlexei Fedorov/* ------------------------------------------------------------------ 394ed108b56SAlexei Fedorov * This routine assumes that the SP_EL3 is pointing to a valid 395ed108b56SAlexei Fedorov * context structure from where the gp regs and other special 396ed108b56SAlexei Fedorov * registers can be retrieved. 397ed108b56SAlexei Fedorov * ------------------------------------------------------------------ 398532ed618SSoby Mathew */ 399532ed618SSoby Mathewfunc el3_exit 400bb9549baSJan Dabros#if ENABLE_ASSERTIONS 401bb9549baSJan Dabros /* el3_exit assumes SP_EL0 on entry */ 402bb9549baSJan Dabros mrs x17, spsel 403bb9549baSJan Dabros cmp x17, #MODE_SP_EL0 404bb9549baSJan Dabros ASM_ASSERT(eq) 4050ce220afSJayanth Dodderi Chidanand#endif /* ENABLE_ASSERTIONS */ 406bb9549baSJan Dabros 407ed108b56SAlexei Fedorov /* ---------------------------------------------------------- 408ed108b56SAlexei Fedorov * Save the current SP_EL0 i.e. the EL3 runtime stack which 409ed108b56SAlexei Fedorov * will be used for handling the next SMC. 410ed108b56SAlexei Fedorov * Then switch to SP_EL3. 411ed108b56SAlexei Fedorov * ---------------------------------------------------------- 412532ed618SSoby Mathew */ 413532ed618SSoby Mathew mov x17, sp 414ed108b56SAlexei Fedorov msr spsel, #MODE_SP_ELX 415532ed618SSoby Mathew str x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP] 416532ed618SSoby Mathew 4170c5e7d1cSMax Shvetsov /* ---------------------------------------------------------- 41868ac5ed0SArunachalam Ganapathy * Restore CPTR_EL3. 4190c5e7d1cSMax Shvetsov * ZCR is only restored if SVE is supported and enabled. 4200c5e7d1cSMax Shvetsov * Synchronization is required before zcr_el3 is addressed. 4210c5e7d1cSMax Shvetsov * ---------------------------------------------------------- 4220c5e7d1cSMax Shvetsov */ 423461c0a5dSElizabeth Ho 424461c0a5dSElizabeth Ho /* The address of the per_world context is stored in x9 */ 425461c0a5dSElizabeth Ho get_per_world_context x9 426461c0a5dSElizabeth Ho 427461c0a5dSElizabeth Ho ldp x19, x20, [x9, #CTX_CPTR_EL3] 4280c5e7d1cSMax Shvetsov msr cptr_el3, x19 4290c5e7d1cSMax Shvetsov 430f0c96a2eSBoyan Karatotev#if IMAGE_BL31 4310c5e7d1cSMax Shvetsov ands x19, x19, #CPTR_EZ_BIT 4320c5e7d1cSMax Shvetsov beq sve_not_enabled 4330c5e7d1cSMax Shvetsov 4340c5e7d1cSMax Shvetsov isb 4350c5e7d1cSMax Shvetsov msr S3_6_C1_C2_0, x20 /* zcr_el3 */ 4360c5e7d1cSMax Shvetsovsve_not_enabled: 437edebefbcSArvind Ram Prakash 438edebefbcSArvind Ram Prakash restore_mpam3_el3 439edebefbcSArvind Ram Prakash 4400ce220afSJayanth Dodderi Chidanand#endif /* IMAGE_BL31 */ 4410c5e7d1cSMax Shvetsov 442fe007b2eSDimitris Papastamos#if IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639 443ed108b56SAlexei Fedorov /* ---------------------------------------------------------- 444ed108b56SAlexei Fedorov * Restore mitigation state as it was on entry to EL3 445ed108b56SAlexei Fedorov * ---------------------------------------------------------- 446ed108b56SAlexei Fedorov */ 447fe007b2eSDimitris Papastamos ldr x17, [sp, #CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_DISABLE] 448ed108b56SAlexei Fedorov cbz x17, 1f 449fe007b2eSDimitris Papastamos blr x17 4504d1ccf0eSAntonio Nino Diaz1: 4510ce220afSJayanth Dodderi Chidanand#endif /* IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639 */ 4520ce220afSJayanth Dodderi Chidanand 4536597fcf1SManish Pandey#if IMAGE_BL31 4546597fcf1SManish Pandey synchronize_errors 4556597fcf1SManish Pandey#endif /* IMAGE_BL31 */ 4560ce220afSJayanth Dodderi Chidanand 457*123002f9SJayanth Dodderi Chidanand /* -------------------------------------------------------------- 458*123002f9SJayanth Dodderi Chidanand * Restore MDCR_EL3, SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET 459*123002f9SJayanth Dodderi Chidanand * -------------------------------------------------------------- 460ff1d2ef3SManish Pandey */ 461ff1d2ef3SManish Pandey ldp x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3] 462*123002f9SJayanth Dodderi Chidanand ldr x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3] 463*123002f9SJayanth Dodderi Chidanand ldr x19, [sp, #CTX_EL3STATE_OFFSET + CTX_MDCR_EL3] 464ff1d2ef3SManish Pandey msr spsr_el3, x16 465ff1d2ef3SManish Pandey msr elr_el3, x17 466*123002f9SJayanth Dodderi Chidanand msr scr_el3, x18 467*123002f9SJayanth Dodderi Chidanand msr mdcr_el3, x19 468ff1d2ef3SManish Pandey 469ff1d2ef3SManish Pandey restore_ptw_el1_sys_regs 470ff1d2ef3SManish Pandey 471ff1d2ef3SManish Pandey /* ---------------------------------------------------------- 472ff1d2ef3SManish Pandey * Restore general purpose (including x30), PMCR_EL0 and 473ff1d2ef3SManish Pandey * ARMv8.3-PAuth registers. 474ff1d2ef3SManish Pandey * Exit EL3 via ERET to a lower exception level. 475ff1d2ef3SManish Pandey * ---------------------------------------------------------- 476ff1d2ef3SManish Pandey */ 477ff1d2ef3SManish Pandey bl restore_gp_pmcr_pauth_regs 478ff1d2ef3SManish Pandey ldr x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR] 479ff1d2ef3SManish Pandey 480c2d32a5fSMadhukar Pappireddy#ifdef IMAGE_BL31 481d04c04a4SManish Pandey /* Clear the EL3 flag as we are exiting el3 */ 482d04c04a4SManish Pandey str xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_NESTED_EA_FLAG] 4830ce220afSJayanth Dodderi Chidanand#endif /* IMAGE_BL31 */ 4840ce220afSJayanth Dodderi Chidanand 485f461fe34SAnthony Steinhauser exception_return 4865283962eSAntonio Nino Diaz 487532ed618SSoby Mathewendfunc el3_exit 488