Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +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 | |
Achin Gupta | 27b895e | 2014-05-04 18:38:28 +0100 | [diff] [blame] | 31 | #include <arch.h> |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 32 | #include <arch_helpers.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <assert.h> |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 34 | #include <bl_common.h> |
Soby Mathew | 5e5c207 | 2014-04-07 15:28:55 +0100 | [diff] [blame] | 35 | #include <bl31.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 36 | #include <context.h> |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 37 | #include <context_mgmt.h> |
Andrew Thoelke | c02dbd6 | 2014-06-02 10:00:25 +0100 | [diff] [blame] | 38 | #include <cpu_data.h> |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 39 | #include <interrupt_mgmt.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 40 | #include <platform.h> |
Dan Handley | ed6ff95 | 2014-05-14 17:44:19 +0100 | [diff] [blame] | 41 | #include <platform_def.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 42 | #include <runtime_svc.h> |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 43 | #include <string.h> |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 44 | |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 45 | |
| 46 | /******************************************************************************* |
| 47 | * Context management library initialisation routine. This library is used by |
| 48 | * runtime services to share pointers to 'cpu_context' structures for the secure |
| 49 | * and non-secure states. Management of the structures and their associated |
| 50 | * memory is not done by the context management library e.g. the PSCI service |
| 51 | * manages the cpu context used for entry from and exit to the non-secure state. |
| 52 | * The Secure payload dispatcher service manages the context(s) corresponding to |
| 53 | * the secure state. It also uses this library to get access to the non-secure |
| 54 | * state cpu context pointers. |
| 55 | * Lastly, this library provides the api to make SP_EL3 point to the cpu context |
| 56 | * which will used for programming an entry into a lower EL. The same context |
| 57 | * will used to save state upon exception entry from that EL. |
| 58 | ******************************************************************************/ |
Juan Castillo | 2d55240 | 2014-06-13 17:05:10 +0100 | [diff] [blame] | 59 | void cm_init(void) |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 60 | { |
| 61 | /* |
| 62 | * The context management library has only global data to intialize, but |
| 63 | * that will be done when the BSS is zeroed out |
| 64 | */ |
| 65 | } |
| 66 | |
| 67 | /******************************************************************************* |
| 68 | * This function returns a pointer to the most recent 'cpu_context' structure |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 69 | * for the CPU identified by MPIDR that was set as the context for the specified |
| 70 | * security state. NULL is returned if no such structure has been specified. |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 71 | ******************************************************************************/ |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 72 | void *cm_get_context_by_mpidr(uint64_t mpidr, uint32_t security_state) |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 73 | { |
Juan Castillo | f558cac | 2014-06-05 09:45:36 +0100 | [diff] [blame] | 74 | assert(sec_state_is_valid(security_state)); |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 75 | |
Andrew Thoelke | c02dbd6 | 2014-06-02 10:00:25 +0100 | [diff] [blame] | 76 | return get_cpu_data_by_mpidr(mpidr, cpu_context[security_state]); |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /******************************************************************************* |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 80 | * This function sets the pointer to the current 'cpu_context' structure for the |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 81 | * specified security state for the CPU identified by MPIDR |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 82 | ******************************************************************************/ |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 83 | void cm_set_context_by_mpidr(uint64_t mpidr, void *context, uint32_t security_state) |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 84 | { |
Juan Castillo | f558cac | 2014-06-05 09:45:36 +0100 | [diff] [blame] | 85 | assert(sec_state_is_valid(security_state)); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 86 | |
Andrew Thoelke | c02dbd6 | 2014-06-02 10:00:25 +0100 | [diff] [blame] | 87 | set_cpu_data_by_mpidr(mpidr, cpu_context[security_state], context); |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /******************************************************************************* |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 91 | * This function is used to program the context that's used for exception |
| 92 | * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for |
| 93 | * the required security state |
| 94 | ******************************************************************************/ |
| 95 | static inline void cm_set_next_context(void *context) |
| 96 | { |
| 97 | #if DEBUG |
| 98 | uint64_t sp_mode; |
| 99 | |
| 100 | /* |
| 101 | * Check that this function is called with SP_EL0 as the stack |
| 102 | * pointer |
| 103 | */ |
| 104 | __asm__ volatile("mrs %0, SPSel\n" |
| 105 | : "=r" (sp_mode)); |
| 106 | |
| 107 | assert(sp_mode == MODE_SP_EL0); |
| 108 | #endif |
| 109 | |
| 110 | __asm__ volatile("msr spsel, #1\n" |
| 111 | "mov sp, %0\n" |
| 112 | "msr spsel, #0\n" |
| 113 | : : "r" (context)); |
| 114 | } |
| 115 | |
| 116 | /******************************************************************************* |
| 117 | * The following function initializes a cpu_context for the current CPU for |
| 118 | * first use, and sets the initial entrypoint state as specified by the |
| 119 | * entry_point_info structure. |
| 120 | * |
| 121 | * The security state to initialize is determined by the SECURE attribute |
| 122 | * of the entry_point_info. The function returns a pointer to the initialized |
| 123 | * context and sets this as the next context to return to. |
| 124 | * |
| 125 | * The EE and ST attributes are used to configure the endianess and secure |
| 126 | * timer availability for the new excution context. |
| 127 | * |
| 128 | * To prepare the register state for entry call cm_prepare_el3_exit() and |
| 129 | * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to |
| 130 | * cm_e1_sysreg_context_restore(). |
| 131 | ******************************************************************************/ |
| 132 | void cm_init_context(uint64_t mpidr, const entry_point_info_t *ep) |
| 133 | { |
| 134 | uint32_t security_state; |
| 135 | cpu_context_t *ctx; |
| 136 | uint32_t scr_el3; |
| 137 | el3_state_t *state; |
| 138 | gp_regs_t *gp_regs; |
| 139 | unsigned long sctlr_elx; |
| 140 | |
| 141 | security_state = GET_SECURITY_STATE(ep->h.attr); |
| 142 | ctx = cm_get_context_by_mpidr(mpidr, security_state); |
| 143 | assert(ctx); |
| 144 | |
| 145 | /* Clear any residual register values from the context */ |
| 146 | memset(ctx, 0, sizeof(*ctx)); |
| 147 | |
| 148 | /* |
| 149 | * Base the context SCR on the current value, adjust for entry point |
| 150 | * specific requirements and set trap bits from the IMF |
| 151 | * TODO: provide the base/global SCR bits using another mechanism? |
| 152 | */ |
| 153 | scr_el3 = read_scr(); |
| 154 | scr_el3 &= ~(SCR_NS_BIT | SCR_RW_BIT | SCR_FIQ_BIT | SCR_IRQ_BIT | |
| 155 | SCR_ST_BIT | SCR_HCE_BIT); |
| 156 | |
| 157 | if (security_state != SECURE) |
| 158 | scr_el3 |= SCR_NS_BIT; |
| 159 | |
| 160 | if (GET_RW(ep->spsr) == MODE_RW_64) |
| 161 | scr_el3 |= SCR_RW_BIT; |
| 162 | |
| 163 | if (EP_GET_ST(ep->h.attr)) |
| 164 | scr_el3 |= SCR_ST_BIT; |
| 165 | |
| 166 | scr_el3 |= get_scr_el3_from_routing_model(security_state); |
| 167 | |
| 168 | /* |
| 169 | * Set up SCTLR_ELx for the target exception level: |
| 170 | * EE bit is taken from the entrpoint attributes |
| 171 | * M, C and I bits must be zero (as required by PSCI specification) |
| 172 | * |
| 173 | * The target exception level is based on the spsr mode requested. |
| 174 | * If execution is requested to EL2 or hyp mode, HVC is enabled |
| 175 | * via SCR_EL3.HCE. |
| 176 | * |
| 177 | * Always compute the SCTLR_EL1 value and save in the cpu_context |
| 178 | * - the EL2 registers are set up by cm_preapre_ns_entry() as they |
| 179 | * are not part of the stored cpu_context |
| 180 | * |
| 181 | * TODO: In debug builds the spsr should be validated and checked |
| 182 | * against the CPU support, security state, endianess and pc |
| 183 | */ |
| 184 | sctlr_elx = EP_GET_EE(ep->h.attr) ? SCTLR_EE_BIT : 0; |
| 185 | sctlr_elx |= SCTLR_EL1_RES1; |
| 186 | write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx); |
| 187 | |
| 188 | if ((GET_RW(ep->spsr) == MODE_RW_64 |
| 189 | && GET_EL(ep->spsr) == MODE_EL2) |
| 190 | || (GET_RW(ep->spsr) != MODE_RW_64 |
| 191 | && GET_M32(ep->spsr) == MODE32_hyp)) { |
| 192 | scr_el3 |= SCR_HCE_BIT; |
| 193 | } |
| 194 | |
| 195 | /* Populate EL3 state so that we've the right context before doing ERET */ |
| 196 | state = get_el3state_ctx(ctx); |
| 197 | write_ctx_reg(state, CTX_SCR_EL3, scr_el3); |
| 198 | write_ctx_reg(state, CTX_ELR_EL3, ep->pc); |
| 199 | write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr); |
| 200 | |
| 201 | /* |
| 202 | * Store the X0-X7 value from the entrypoint into the context |
| 203 | * Use memcpy as we are in control of the layout of the structures |
| 204 | */ |
| 205 | gp_regs = get_gpregs_ctx(ctx); |
| 206 | memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t)); |
| 207 | } |
| 208 | |
| 209 | /******************************************************************************* |
| 210 | * Prepare the CPU system registers for first entry into secure or normal world |
| 211 | * |
| 212 | * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized |
| 213 | * If execution is requested to non-secure EL1 or svc mode, and the CPU supports |
| 214 | * EL2 then EL2 is disabled by configuring all necessary EL2 registers. |
| 215 | * For all entries, the EL1 registers are initialized from the cpu_context |
| 216 | ******************************************************************************/ |
| 217 | void cm_prepare_el3_exit(uint32_t security_state) |
| 218 | { |
| 219 | uint32_t sctlr_elx, scr_el3, cptr_el2; |
| 220 | cpu_context_t *ctx = cm_get_context(security_state); |
| 221 | |
| 222 | assert(ctx); |
| 223 | |
| 224 | if (security_state == NON_SECURE) { |
| 225 | scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3); |
| 226 | if (scr_el3 & SCR_HCE_BIT) { |
| 227 | /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */ |
| 228 | sctlr_elx = read_ctx_reg(get_sysregs_ctx(ctx), |
| 229 | CTX_SCTLR_EL1); |
| 230 | sctlr_elx &= ~SCTLR_EE_BIT; |
| 231 | sctlr_elx |= SCTLR_EL2_RES1; |
| 232 | write_sctlr_el2(sctlr_elx); |
| 233 | } else if (read_id_aa64pfr0_el1() & |
| 234 | (ID_AA64PFR0_ELX_MASK << ID_AA64PFR0_EL2_SHIFT)) { |
| 235 | /* EL2 present but unused, need to disable safely */ |
| 236 | |
| 237 | /* HCR_EL2 = 0, except RW bit set to match SCR_EL3 */ |
| 238 | write_hcr_el2((scr_el3 & SCR_RW_BIT) ? HCR_RW_BIT : 0); |
| 239 | |
| 240 | /* SCTLR_EL2 : can be ignored when bypassing */ |
| 241 | |
| 242 | /* CPTR_EL2 : disable all traps TCPAC, TTA, TFP */ |
| 243 | cptr_el2 = read_cptr_el2(); |
| 244 | cptr_el2 &= ~(TCPAC_BIT | TTA_BIT | TFP_BIT); |
| 245 | write_cptr_el2(cptr_el2); |
| 246 | |
| 247 | /* Enable EL1 access to timer */ |
| 248 | write_cnthctl_el2(EL1PCEN_BIT | EL1PCTEN_BIT); |
| 249 | |
| 250 | /* Set VPIDR, VMPIDR to match MIDR, MPIDR */ |
| 251 | write_vpidr_el2(read_midr_el1()); |
| 252 | write_vmpidr_el2(read_mpidr_el1()); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | el1_sysregs_context_restore(get_sysregs_ctx(ctx)); |
| 257 | |
| 258 | cm_set_next_context(ctx); |
| 259 | } |
| 260 | |
| 261 | /******************************************************************************* |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 262 | * The next four functions are used by runtime services to save and restore EL3 |
| 263 | * and EL1 contexts on the 'cpu_context' structure for the specified security |
| 264 | * state. |
| 265 | ******************************************************************************/ |
| 266 | void cm_el3_sysregs_context_save(uint32_t security_state) |
| 267 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 268 | cpu_context_t *ctx; |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 269 | |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 270 | ctx = cm_get_context(security_state); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 271 | assert(ctx); |
| 272 | |
| 273 | el3_sysregs_context_save(get_el3state_ctx(ctx)); |
| 274 | } |
| 275 | |
| 276 | void cm_el3_sysregs_context_restore(uint32_t security_state) |
| 277 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 278 | cpu_context_t *ctx; |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 279 | |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 280 | ctx = cm_get_context(security_state); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 281 | assert(ctx); |
| 282 | |
| 283 | el3_sysregs_context_restore(get_el3state_ctx(ctx)); |
| 284 | } |
| 285 | |
| 286 | void cm_el1_sysregs_context_save(uint32_t security_state) |
| 287 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 288 | cpu_context_t *ctx; |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 289 | |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 290 | ctx = cm_get_context(security_state); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 291 | assert(ctx); |
| 292 | |
| 293 | el1_sysregs_context_save(get_sysregs_ctx(ctx)); |
| 294 | } |
| 295 | |
| 296 | void cm_el1_sysregs_context_restore(uint32_t security_state) |
| 297 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 298 | cpu_context_t *ctx; |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 299 | |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 300 | ctx = cm_get_context(security_state); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 301 | assert(ctx); |
| 302 | |
| 303 | el1_sysregs_context_restore(get_sysregs_ctx(ctx)); |
| 304 | } |
| 305 | |
| 306 | /******************************************************************************* |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 307 | * This function populates ELR_EL3 member of 'cpu_context' pertaining to the |
| 308 | * given security state with the given entrypoint |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 309 | ******************************************************************************/ |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 310 | void cm_set_elr_el3(uint32_t security_state, uint64_t entrypoint) |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 311 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 312 | cpu_context_t *ctx; |
| 313 | el3_state_t *state; |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 314 | |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 315 | ctx = cm_get_context(security_state); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 316 | assert(ctx); |
| 317 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 318 | /* Populate EL3 state so that ERET jumps to the correct entry */ |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 319 | state = get_el3state_ctx(ctx); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 320 | write_ctx_reg(state, CTX_ELR_EL3, entrypoint); |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /******************************************************************************* |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 324 | * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context' |
| 325 | * pertaining to the given security state |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 326 | ******************************************************************************/ |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 327 | void cm_set_elr_spsr_el3(uint32_t security_state, |
| 328 | uint64_t entrypoint, uint32_t spsr) |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 329 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 330 | cpu_context_t *ctx; |
| 331 | el3_state_t *state; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 332 | |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 333 | ctx = cm_get_context(security_state); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 334 | assert(ctx); |
| 335 | |
| 336 | /* Populate EL3 state so that ERET jumps to the correct entry */ |
| 337 | state = get_el3state_ctx(ctx); |
| 338 | write_ctx_reg(state, CTX_ELR_EL3, entrypoint); |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 339 | write_ctx_reg(state, CTX_SPSR_EL3, spsr); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /******************************************************************************* |
Achin Gupta | 27b895e | 2014-05-04 18:38:28 +0100 | [diff] [blame] | 343 | * This function updates a single bit in the SCR_EL3 member of the 'cpu_context' |
| 344 | * pertaining to the given security state using the value and bit position |
| 345 | * specified in the parameters. It preserves all other bits. |
| 346 | ******************************************************************************/ |
| 347 | void cm_write_scr_el3_bit(uint32_t security_state, |
| 348 | uint32_t bit_pos, |
| 349 | uint32_t value) |
| 350 | { |
| 351 | cpu_context_t *ctx; |
| 352 | el3_state_t *state; |
| 353 | uint32_t scr_el3; |
| 354 | |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 355 | ctx = cm_get_context(security_state); |
Achin Gupta | 27b895e | 2014-05-04 18:38:28 +0100 | [diff] [blame] | 356 | assert(ctx); |
| 357 | |
| 358 | /* Ensure that the bit position is a valid one */ |
| 359 | assert((1 << bit_pos) & SCR_VALID_BIT_MASK); |
| 360 | |
| 361 | /* Ensure that the 'value' is only a bit wide */ |
| 362 | assert(value <= 1); |
| 363 | |
| 364 | /* |
| 365 | * Get the SCR_EL3 value from the cpu context, clear the desired bit |
| 366 | * and set it to its new value. |
| 367 | */ |
| 368 | state = get_el3state_ctx(ctx); |
| 369 | scr_el3 = read_ctx_reg(state, CTX_SCR_EL3); |
| 370 | scr_el3 &= ~(1 << bit_pos); |
| 371 | scr_el3 |= value << bit_pos; |
| 372 | write_ctx_reg(state, CTX_SCR_EL3, scr_el3); |
| 373 | } |
| 374 | |
| 375 | /******************************************************************************* |
| 376 | * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the |
| 377 | * given security state. |
| 378 | ******************************************************************************/ |
| 379 | uint32_t cm_get_scr_el3(uint32_t security_state) |
| 380 | { |
| 381 | cpu_context_t *ctx; |
| 382 | el3_state_t *state; |
| 383 | |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 384 | ctx = cm_get_context(security_state); |
Achin Gupta | 27b895e | 2014-05-04 18:38:28 +0100 | [diff] [blame] | 385 | assert(ctx); |
| 386 | |
| 387 | /* Populate EL3 state so that ERET jumps to the correct entry */ |
| 388 | state = get_el3state_ctx(ctx); |
| 389 | return read_ctx_reg(state, CTX_SCR_EL3); |
| 390 | } |
| 391 | |
| 392 | /******************************************************************************* |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 393 | * This function is used to program the context that's used for exception |
| 394 | * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for |
| 395 | * the required security state |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 396 | ******************************************************************************/ |
| 397 | void cm_set_next_eret_context(uint32_t security_state) |
| 398 | { |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 399 | cpu_context_t *ctx; |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 400 | |
Andrew Thoelke | a2f6553 | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 401 | ctx = cm_get_context(security_state); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 402 | assert(ctx); |
| 403 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 404 | cm_set_next_context(ctx); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 405 | } |