Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 1 | /* |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 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 | c429b5e | 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 | 97043ac | 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 | a43d431 | 2014-04-07 15:28:55 +0100 | [diff] [blame] | 35 | #include <bl31.h> |
Dan Handley | 97043ac | 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 | aaba4f2 | 2014-06-02 10:00:25 +0100 | [diff] [blame] | 38 | #include <cpu_data.h> |
Achin Gupta | e1333f7 | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 39 | #include <interrupt_mgmt.h> |
Dan Handley | 97043ac | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 40 | #include <platform.h> |
Dan Handley | 5f0cdb0 | 2014-05-14 17:44:19 +0100 | [diff] [blame] | 41 | #include <platform_def.h> |
Dan Handley | 97043ac | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 42 | #include <runtime_svc.h> |
Andrew Thoelke | 167a935 | 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 | 4f2104f | 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 |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 69 | * for the CPU identified by `cpu_idx` that was set as the context for the |
| 70 | * specified security state. NULL is returned if no such structure has been |
| 71 | * specified. |
| 72 | ******************************************************************************/ |
| 73 | void *cm_get_context_by_index(unsigned int cpu_idx, |
| 74 | unsigned int security_state) |
| 75 | { |
| 76 | assert(sec_state_is_valid(security_state)); |
| 77 | |
| 78 | return get_cpu_data_by_index(cpu_idx, cpu_context[security_state]); |
| 79 | } |
| 80 | |
| 81 | /******************************************************************************* |
| 82 | * This function sets the pointer to the current 'cpu_context' structure for the |
| 83 | * specified security state for the CPU identified by CPU index. |
| 84 | ******************************************************************************/ |
| 85 | void cm_set_context_by_index(unsigned int cpu_idx, void *context, |
| 86 | unsigned int security_state) |
| 87 | { |
| 88 | assert(sec_state_is_valid(security_state)); |
| 89 | |
| 90 | set_cpu_data_by_index(cpu_idx, cpu_context[security_state], context); |
| 91 | } |
| 92 | |
| 93 | /******************************************************************************* |
| 94 | * This function returns a pointer to the most recent 'cpu_context' structure |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 95 | * for the CPU identified by MPIDR that was set as the context for the specified |
| 96 | * security state. NULL is returned if no such structure has been specified. |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 97 | ******************************************************************************/ |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 98 | 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] | 99 | { |
Juan Castillo | d3280be | 2014-06-05 09:45:36 +0100 | [diff] [blame] | 100 | assert(sec_state_is_valid(security_state)); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 101 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 102 | return cm_get_context_by_index(platform_get_core_pos(mpidr), security_state); |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /******************************************************************************* |
| 106 | * This function sets the pointer to the current 'cpu_context' structure for the |
| 107 | * specified security state for the CPU identified by MPIDR |
| 108 | ******************************************************************************/ |
| 109 | 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] | 110 | { |
Juan Castillo | d3280be | 2014-06-05 09:45:36 +0100 | [diff] [blame] | 111 | assert(sec_state_is_valid(security_state)); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 112 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 113 | cm_set_context_by_index(platform_get_core_pos(mpidr), |
| 114 | context, security_state); |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /******************************************************************************* |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 118 | * This function is used to program the context that's used for exception |
| 119 | * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for |
| 120 | * the required security state |
| 121 | ******************************************************************************/ |
| 122 | static inline void cm_set_next_context(void *context) |
| 123 | { |
| 124 | #if DEBUG |
| 125 | uint64_t sp_mode; |
| 126 | |
| 127 | /* |
| 128 | * Check that this function is called with SP_EL0 as the stack |
| 129 | * pointer |
| 130 | */ |
| 131 | __asm__ volatile("mrs %0, SPSel\n" |
| 132 | : "=r" (sp_mode)); |
| 133 | |
| 134 | assert(sp_mode == MODE_SP_EL0); |
| 135 | #endif |
| 136 | |
| 137 | __asm__ volatile("msr spsel, #1\n" |
| 138 | "mov sp, %0\n" |
| 139 | "msr spsel, #0\n" |
| 140 | : : "r" (context)); |
| 141 | } |
| 142 | |
| 143 | /******************************************************************************* |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 144 | * The following function initializes the cpu_context 'ctx' for |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 145 | * first use, and sets the initial entrypoint state as specified by the |
| 146 | * entry_point_info structure. |
| 147 | * |
| 148 | * The security state to initialize is determined by the SECURE attribute |
| 149 | * of the entry_point_info. The function returns a pointer to the initialized |
| 150 | * context and sets this as the next context to return to. |
| 151 | * |
| 152 | * The EE and ST attributes are used to configure the endianess and secure |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 153 | * timer availability for the new execution context. |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 154 | * |
| 155 | * To prepare the register state for entry call cm_prepare_el3_exit() and |
| 156 | * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to |
| 157 | * cm_e1_sysreg_context_restore(). |
| 158 | ******************************************************************************/ |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 159 | static void cm_init_context_common(cpu_context_t *ctx, const entry_point_info_t *ep) |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 160 | { |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 161 | unsigned int security_state; |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 162 | uint32_t scr_el3; |
| 163 | el3_state_t *state; |
| 164 | gp_regs_t *gp_regs; |
| 165 | unsigned long sctlr_elx; |
| 166 | |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 167 | assert(ctx); |
| 168 | |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 169 | security_state = GET_SECURITY_STATE(ep->h.attr); |
| 170 | |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 171 | /* Clear any residual register values from the context */ |
| 172 | memset(ctx, 0, sizeof(*ctx)); |
| 173 | |
| 174 | /* |
| 175 | * Base the context SCR on the current value, adjust for entry point |
| 176 | * specific requirements and set trap bits from the IMF |
| 177 | * TODO: provide the base/global SCR bits using another mechanism? |
| 178 | */ |
| 179 | scr_el3 = read_scr(); |
| 180 | scr_el3 &= ~(SCR_NS_BIT | SCR_RW_BIT | SCR_FIQ_BIT | SCR_IRQ_BIT | |
| 181 | SCR_ST_BIT | SCR_HCE_BIT); |
| 182 | |
| 183 | if (security_state != SECURE) |
| 184 | scr_el3 |= SCR_NS_BIT; |
| 185 | |
| 186 | if (GET_RW(ep->spsr) == MODE_RW_64) |
| 187 | scr_el3 |= SCR_RW_BIT; |
| 188 | |
| 189 | if (EP_GET_ST(ep->h.attr)) |
| 190 | scr_el3 |= SCR_ST_BIT; |
| 191 | |
| 192 | scr_el3 |= get_scr_el3_from_routing_model(security_state); |
| 193 | |
| 194 | /* |
| 195 | * Set up SCTLR_ELx for the target exception level: |
| 196 | * EE bit is taken from the entrpoint attributes |
| 197 | * M, C and I bits must be zero (as required by PSCI specification) |
| 198 | * |
| 199 | * The target exception level is based on the spsr mode requested. |
| 200 | * If execution is requested to EL2 or hyp mode, HVC is enabled |
| 201 | * via SCR_EL3.HCE. |
| 202 | * |
| 203 | * Always compute the SCTLR_EL1 value and save in the cpu_context |
| 204 | * - the EL2 registers are set up by cm_preapre_ns_entry() as they |
| 205 | * are not part of the stored cpu_context |
| 206 | * |
| 207 | * TODO: In debug builds the spsr should be validated and checked |
| 208 | * against the CPU support, security state, endianess and pc |
| 209 | */ |
| 210 | sctlr_elx = EP_GET_EE(ep->h.attr) ? SCTLR_EE_BIT : 0; |
Jens Wiklander | ae213ce | 2014-09-04 10:23:27 +0200 | [diff] [blame] | 211 | if (GET_RW(ep->spsr) == MODE_RW_64) |
| 212 | sctlr_elx |= SCTLR_EL1_RES1; |
| 213 | else |
| 214 | sctlr_elx |= SCTLR_AARCH32_EL1_RES1; |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 215 | write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx); |
| 216 | |
| 217 | if ((GET_RW(ep->spsr) == MODE_RW_64 |
| 218 | && GET_EL(ep->spsr) == MODE_EL2) |
| 219 | || (GET_RW(ep->spsr) != MODE_RW_64 |
| 220 | && GET_M32(ep->spsr) == MODE32_hyp)) { |
| 221 | scr_el3 |= SCR_HCE_BIT; |
| 222 | } |
| 223 | |
| 224 | /* Populate EL3 state so that we've the right context before doing ERET */ |
| 225 | state = get_el3state_ctx(ctx); |
| 226 | write_ctx_reg(state, CTX_SCR_EL3, scr_el3); |
| 227 | write_ctx_reg(state, CTX_ELR_EL3, ep->pc); |
| 228 | write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr); |
| 229 | |
| 230 | /* |
| 231 | * Store the X0-X7 value from the entrypoint into the context |
| 232 | * Use memcpy as we are in control of the layout of the structures |
| 233 | */ |
| 234 | gp_regs = get_gpregs_ctx(ctx); |
| 235 | memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t)); |
| 236 | } |
| 237 | |
| 238 | /******************************************************************************* |
Soby Mathew | 12d0d00 | 2015-04-09 13:40:55 +0100 | [diff] [blame^] | 239 | * The following function initializes the cpu_context for a CPU specified by |
| 240 | * its `cpu_idx` for first use, and sets the initial entrypoint state as |
| 241 | * specified by the entry_point_info structure. |
| 242 | ******************************************************************************/ |
| 243 | void cm_init_context_by_index(unsigned int cpu_idx, |
| 244 | const entry_point_info_t *ep) |
| 245 | { |
| 246 | cpu_context_t *ctx; |
| 247 | ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr)); |
| 248 | cm_init_context_common(ctx, ep); |
| 249 | } |
| 250 | |
| 251 | /******************************************************************************* |
| 252 | * The following function initializes the cpu_context for the current CPU |
| 253 | * for first use, and sets the initial entrypoint state as specified by the |
| 254 | * entry_point_info structure. |
| 255 | ******************************************************************************/ |
| 256 | void cm_init_my_context(const entry_point_info_t *ep) |
| 257 | { |
| 258 | cpu_context_t *ctx; |
| 259 | ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr)); |
| 260 | cm_init_context_common(ctx, ep); |
| 261 | } |
| 262 | |
| 263 | /******************************************************************************* |
| 264 | * The following function provides a compatibility function for SPDs using the |
| 265 | * existing cm library routines. This function is expected to be invoked for |
| 266 | * initializing the cpu_context for the CPU specified by MPIDR for first use. |
| 267 | ******************************************************************************/ |
| 268 | void cm_init_context(unsigned long mpidr, const entry_point_info_t *ep) |
| 269 | { |
| 270 | if ((mpidr & MPIDR_AFFINITY_MASK) == |
| 271 | (read_mpidr_el1() & MPIDR_AFFINITY_MASK)) |
| 272 | cm_init_my_context(ep); |
| 273 | else |
| 274 | cm_init_context_by_index(platform_get_core_pos(mpidr), ep); |
| 275 | } |
| 276 | |
| 277 | /******************************************************************************* |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 278 | * Prepare the CPU system registers for first entry into secure or normal world |
| 279 | * |
| 280 | * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized |
| 281 | * If execution is requested to non-secure EL1 or svc mode, and the CPU supports |
| 282 | * EL2 then EL2 is disabled by configuring all necessary EL2 registers. |
| 283 | * For all entries, the EL1 registers are initialized from the cpu_context |
| 284 | ******************************************************************************/ |
| 285 | void cm_prepare_el3_exit(uint32_t security_state) |
| 286 | { |
| 287 | uint32_t sctlr_elx, scr_el3, cptr_el2; |
| 288 | cpu_context_t *ctx = cm_get_context(security_state); |
| 289 | |
| 290 | assert(ctx); |
| 291 | |
| 292 | if (security_state == NON_SECURE) { |
| 293 | scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3); |
| 294 | if (scr_el3 & SCR_HCE_BIT) { |
| 295 | /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */ |
| 296 | sctlr_elx = read_ctx_reg(get_sysregs_ctx(ctx), |
| 297 | CTX_SCTLR_EL1); |
| 298 | sctlr_elx &= ~SCTLR_EE_BIT; |
| 299 | sctlr_elx |= SCTLR_EL2_RES1; |
| 300 | write_sctlr_el2(sctlr_elx); |
| 301 | } else if (read_id_aa64pfr0_el1() & |
| 302 | (ID_AA64PFR0_ELX_MASK << ID_AA64PFR0_EL2_SHIFT)) { |
| 303 | /* EL2 present but unused, need to disable safely */ |
| 304 | |
| 305 | /* HCR_EL2 = 0, except RW bit set to match SCR_EL3 */ |
| 306 | write_hcr_el2((scr_el3 & SCR_RW_BIT) ? HCR_RW_BIT : 0); |
| 307 | |
| 308 | /* SCTLR_EL2 : can be ignored when bypassing */ |
| 309 | |
| 310 | /* CPTR_EL2 : disable all traps TCPAC, TTA, TFP */ |
| 311 | cptr_el2 = read_cptr_el2(); |
| 312 | cptr_el2 &= ~(TCPAC_BIT | TTA_BIT | TFP_BIT); |
| 313 | write_cptr_el2(cptr_el2); |
| 314 | |
| 315 | /* Enable EL1 access to timer */ |
| 316 | write_cnthctl_el2(EL1PCEN_BIT | EL1PCTEN_BIT); |
| 317 | |
Soby Mathew | 14c0526 | 2014-08-29 14:41:58 +0100 | [diff] [blame] | 318 | /* Reset CNTVOFF_EL2 */ |
| 319 | write_cntvoff_el2(0); |
| 320 | |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 321 | /* Set VPIDR, VMPIDR to match MIDR, MPIDR */ |
| 322 | write_vpidr_el2(read_midr_el1()); |
| 323 | write_vmpidr_el2(read_mpidr_el1()); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | el1_sysregs_context_restore(get_sysregs_ctx(ctx)); |
| 328 | |
| 329 | cm_set_next_context(ctx); |
| 330 | } |
| 331 | |
| 332 | /******************************************************************************* |
Soby Mathew | fdfabec | 2014-07-04 16:02:26 +0100 | [diff] [blame] | 333 | * The next four functions are used by runtime services to save and restore |
| 334 | * EL1 context on the 'cpu_context' structure for the specified security |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 335 | * state. |
| 336 | ******************************************************************************/ |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 337 | void cm_el1_sysregs_context_save(uint32_t security_state) |
| 338 | { |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 339 | cpu_context_t *ctx; |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 340 | |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 341 | ctx = cm_get_context(security_state); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 342 | assert(ctx); |
| 343 | |
| 344 | el1_sysregs_context_save(get_sysregs_ctx(ctx)); |
| 345 | } |
| 346 | |
| 347 | void cm_el1_sysregs_context_restore(uint32_t security_state) |
| 348 | { |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 349 | cpu_context_t *ctx; |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 350 | |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 351 | ctx = cm_get_context(security_state); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 352 | assert(ctx); |
| 353 | |
| 354 | el1_sysregs_context_restore(get_sysregs_ctx(ctx)); |
| 355 | } |
| 356 | |
| 357 | /******************************************************************************* |
Achin Gupta | c429b5e | 2014-05-04 18:38:28 +0100 | [diff] [blame] | 358 | * This function populates ELR_EL3 member of 'cpu_context' pertaining to the |
| 359 | * given security state with the given entrypoint |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 360 | ******************************************************************************/ |
Achin Gupta | c429b5e | 2014-05-04 18:38:28 +0100 | [diff] [blame] | 361 | void cm_set_elr_el3(uint32_t security_state, uint64_t entrypoint) |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 362 | { |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 363 | cpu_context_t *ctx; |
| 364 | el3_state_t *state; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 365 | |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 366 | ctx = cm_get_context(security_state); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 367 | assert(ctx); |
| 368 | |
| 369 | /* Populate EL3 state so that ERET jumps to the correct entry */ |
| 370 | state = get_el3state_ctx(ctx); |
| 371 | write_ctx_reg(state, CTX_ELR_EL3, entrypoint); |
| 372 | } |
| 373 | |
| 374 | /******************************************************************************* |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 375 | * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context' |
| 376 | * pertaining to the given security state |
| 377 | ******************************************************************************/ |
| 378 | void cm_set_elr_spsr_el3(uint32_t security_state, |
| 379 | uint64_t entrypoint, uint32_t spsr) |
| 380 | { |
| 381 | cpu_context_t *ctx; |
| 382 | el3_state_t *state; |
| 383 | |
| 384 | ctx = cm_get_context(security_state); |
| 385 | assert(ctx); |
| 386 | |
| 387 | /* Populate EL3 state so that ERET jumps to the correct entry */ |
| 388 | state = get_el3state_ctx(ctx); |
| 389 | write_ctx_reg(state, CTX_ELR_EL3, entrypoint); |
| 390 | write_ctx_reg(state, CTX_SPSR_EL3, spsr); |
| 391 | } |
| 392 | |
| 393 | /******************************************************************************* |
Achin Gupta | c429b5e | 2014-05-04 18:38:28 +0100 | [diff] [blame] | 394 | * This function updates a single bit in the SCR_EL3 member of the 'cpu_context' |
| 395 | * pertaining to the given security state using the value and bit position |
| 396 | * specified in the parameters. It preserves all other bits. |
| 397 | ******************************************************************************/ |
| 398 | void cm_write_scr_el3_bit(uint32_t security_state, |
| 399 | uint32_t bit_pos, |
| 400 | uint32_t value) |
| 401 | { |
| 402 | cpu_context_t *ctx; |
| 403 | el3_state_t *state; |
| 404 | uint32_t scr_el3; |
| 405 | |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 406 | ctx = cm_get_context(security_state); |
Achin Gupta | c429b5e | 2014-05-04 18:38:28 +0100 | [diff] [blame] | 407 | assert(ctx); |
| 408 | |
| 409 | /* Ensure that the bit position is a valid one */ |
| 410 | assert((1 << bit_pos) & SCR_VALID_BIT_MASK); |
| 411 | |
| 412 | /* Ensure that the 'value' is only a bit wide */ |
| 413 | assert(value <= 1); |
| 414 | |
| 415 | /* |
| 416 | * Get the SCR_EL3 value from the cpu context, clear the desired bit |
| 417 | * and set it to its new value. |
| 418 | */ |
| 419 | state = get_el3state_ctx(ctx); |
| 420 | scr_el3 = read_ctx_reg(state, CTX_SCR_EL3); |
| 421 | scr_el3 &= ~(1 << bit_pos); |
| 422 | scr_el3 |= value << bit_pos; |
| 423 | write_ctx_reg(state, CTX_SCR_EL3, scr_el3); |
| 424 | } |
| 425 | |
| 426 | /******************************************************************************* |
| 427 | * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the |
| 428 | * given security state. |
| 429 | ******************************************************************************/ |
| 430 | uint32_t cm_get_scr_el3(uint32_t security_state) |
| 431 | { |
| 432 | cpu_context_t *ctx; |
| 433 | el3_state_t *state; |
| 434 | |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 435 | ctx = cm_get_context(security_state); |
Achin Gupta | c429b5e | 2014-05-04 18:38:28 +0100 | [diff] [blame] | 436 | assert(ctx); |
| 437 | |
| 438 | /* Populate EL3 state so that ERET jumps to the correct entry */ |
| 439 | state = get_el3state_ctx(ctx); |
| 440 | return read_ctx_reg(state, CTX_SCR_EL3); |
| 441 | } |
| 442 | |
| 443 | /******************************************************************************* |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 444 | * This function is used to program the context that's used for exception |
| 445 | * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for |
| 446 | * the required security state |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 447 | ******************************************************************************/ |
| 448 | void cm_set_next_eret_context(uint32_t security_state) |
| 449 | { |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 450 | cpu_context_t *ctx; |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 451 | |
Andrew Thoelke | 08ab89d | 2014-05-14 17:09:32 +0100 | [diff] [blame] | 452 | ctx = cm_get_context(security_state); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 453 | assert(ctx); |
| 454 | |
Andrew Thoelke | 167a935 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 455 | cm_set_next_context(ctx); |
Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 456 | } |