| 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 | 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> |
| Dan Handley | 97043ac | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 38 | #include <platform.h> |
| 39 | #include <runtime_svc.h> |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 40 | |
| 41 | /******************************************************************************* |
| 42 | * Data structure which holds the pointers to non-secure and secure security |
| 43 | * state contexts for each cpu. It is aligned to the cache line boundary to |
| 44 | * allow efficient concurrent manipulation of these pointers on different cpus |
| 45 | ******************************************************************************/ |
| 46 | typedef struct { |
| 47 | void *ptr[2]; |
| Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 48 | } __aligned (CACHE_WRITEBACK_GRANULE) context_info_t; |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 49 | |
| Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 50 | static context_info_t cm_context_info[PLATFORM_CORE_COUNT]; |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 51 | |
| Soby Mathew | a43d431 | 2014-04-07 15:28:55 +0100 | [diff] [blame] | 52 | /* The per_cpu_ptr_cache_t space allocation */ |
| 53 | static per_cpu_ptr_cache_t per_cpu_ptr_cache_space[PLATFORM_CORE_COUNT]; |
| 54 | |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 55 | /******************************************************************************* |
| 56 | * Context management library initialisation routine. This library is used by |
| 57 | * runtime services to share pointers to 'cpu_context' structures for the secure |
| 58 | * and non-secure states. Management of the structures and their associated |
| 59 | * memory is not done by the context management library e.g. the PSCI service |
| 60 | * manages the cpu context used for entry from and exit to the non-secure state. |
| 61 | * The Secure payload dispatcher service manages the context(s) corresponding to |
| 62 | * the secure state. It also uses this library to get access to the non-secure |
| 63 | * state cpu context pointers. |
| 64 | * Lastly, this library provides the api to make SP_EL3 point to the cpu context |
| 65 | * which will used for programming an entry into a lower EL. The same context |
| 66 | * will used to save state upon exception entry from that EL. |
| 67 | ******************************************************************************/ |
| 68 | void cm_init() |
| 69 | { |
| 70 | /* |
| 71 | * The context management library has only global data to intialize, but |
| 72 | * that will be done when the BSS is zeroed out |
| 73 | */ |
| 74 | } |
| 75 | |
| 76 | /******************************************************************************* |
| 77 | * This function returns a pointer to the most recent 'cpu_context' structure |
| 78 | * that was set as the context for the specified security state. NULL is |
| 79 | * returned if no such structure has been specified. |
| 80 | ******************************************************************************/ |
| 81 | void *cm_get_context(uint64_t mpidr, uint32_t security_state) |
| 82 | { |
| 83 | uint32_t linear_id = platform_get_core_pos(mpidr); |
| 84 | |
| 85 | assert(security_state <= NON_SECURE); |
| 86 | |
| 87 | return cm_context_info[linear_id].ptr[security_state]; |
| 88 | } |
| 89 | |
| 90 | /******************************************************************************* |
| 91 | * This function sets the pointer to the current 'cpu_context' structure for the |
| 92 | * specified security state. |
| 93 | ******************************************************************************/ |
| 94 | void cm_set_context(uint64_t mpidr, void *context, uint32_t security_state) |
| 95 | { |
| 96 | uint32_t linear_id = platform_get_core_pos(mpidr); |
| 97 | |
| 98 | assert(security_state <= NON_SECURE); |
| 99 | |
| 100 | cm_context_info[linear_id].ptr[security_state] = context; |
| 101 | } |
| 102 | |
| 103 | /******************************************************************************* |
| 104 | * The next four functions are used by runtime services to save and restore EL3 |
| 105 | * and EL1 contexts on the 'cpu_context' structure for the specified security |
| 106 | * state. |
| 107 | ******************************************************************************/ |
| 108 | void cm_el3_sysregs_context_save(uint32_t security_state) |
| 109 | { |
| Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 110 | cpu_context_t *ctx; |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 111 | |
| 112 | ctx = cm_get_context(read_mpidr(), security_state); |
| 113 | assert(ctx); |
| 114 | |
| 115 | el3_sysregs_context_save(get_el3state_ctx(ctx)); |
| 116 | } |
| 117 | |
| 118 | void cm_el3_sysregs_context_restore(uint32_t security_state) |
| 119 | { |
| Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 120 | cpu_context_t *ctx; |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 121 | |
| 122 | ctx = cm_get_context(read_mpidr(), security_state); |
| 123 | assert(ctx); |
| 124 | |
| 125 | el3_sysregs_context_restore(get_el3state_ctx(ctx)); |
| 126 | } |
| 127 | |
| 128 | void cm_el1_sysregs_context_save(uint32_t security_state) |
| 129 | { |
| Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 130 | cpu_context_t *ctx; |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 131 | |
| 132 | ctx = cm_get_context(read_mpidr(), security_state); |
| 133 | assert(ctx); |
| 134 | |
| 135 | el1_sysregs_context_save(get_sysregs_ctx(ctx)); |
| 136 | } |
| 137 | |
| 138 | void cm_el1_sysregs_context_restore(uint32_t security_state) |
| 139 | { |
| Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 140 | cpu_context_t *ctx; |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 141 | |
| 142 | ctx = cm_get_context(read_mpidr(), security_state); |
| 143 | assert(ctx); |
| 144 | |
| 145 | el1_sysregs_context_restore(get_sysregs_ctx(ctx)); |
| 146 | } |
| 147 | |
| 148 | /******************************************************************************* |
| Achin Gupta | c429b5e | 2014-05-04 18:38:28 +0100 | [diff] [blame^] | 149 | * This function populates 'cpu_context' pertaining to the given security state |
| 150 | * with the entrypoint, SPSR and SCR values so that an ERET from this security |
| 151 | * state correctly restores corresponding values to drop the CPU to the next |
| 152 | * exception level |
| Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 153 | ******************************************************************************/ |
| 154 | void cm_set_el3_eret_context(uint32_t security_state, uint64_t entrypoint, |
| 155 | uint32_t spsr, uint32_t scr) |
| 156 | { |
| Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 157 | cpu_context_t *ctx; |
| 158 | el3_state_t *state; |
| Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 159 | |
| 160 | ctx = cm_get_context(read_mpidr(), security_state); |
| 161 | assert(ctx); |
| 162 | |
| 163 | /* Populate EL3 state so that we've the right context before doing ERET */ |
| 164 | state = get_el3state_ctx(ctx); |
| 165 | write_ctx_reg(state, CTX_SPSR_EL3, spsr); |
| 166 | write_ctx_reg(state, CTX_ELR_EL3, entrypoint); |
| 167 | write_ctx_reg(state, CTX_SCR_EL3, scr); |
| 168 | } |
| 169 | |
| 170 | /******************************************************************************* |
| Achin Gupta | c429b5e | 2014-05-04 18:38:28 +0100 | [diff] [blame^] | 171 | * This function populates ELR_EL3 member of 'cpu_context' pertaining to the |
| 172 | * given security state with the given entrypoint |
| Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 173 | ******************************************************************************/ |
| Achin Gupta | c429b5e | 2014-05-04 18:38:28 +0100 | [diff] [blame^] | 174 | void cm_set_elr_el3(uint32_t security_state, uint64_t entrypoint) |
| Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 175 | { |
| Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 176 | cpu_context_t *ctx; |
| 177 | el3_state_t *state; |
| Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 178 | |
| 179 | ctx = cm_get_context(read_mpidr(), security_state); |
| 180 | assert(ctx); |
| 181 | |
| 182 | /* Populate EL3 state so that ERET jumps to the correct entry */ |
| 183 | state = get_el3state_ctx(ctx); |
| 184 | write_ctx_reg(state, CTX_ELR_EL3, entrypoint); |
| 185 | } |
| 186 | |
| 187 | /******************************************************************************* |
| Achin Gupta | c429b5e | 2014-05-04 18:38:28 +0100 | [diff] [blame^] | 188 | * This function updates a single bit in the SCR_EL3 member of the 'cpu_context' |
| 189 | * pertaining to the given security state using the value and bit position |
| 190 | * specified in the parameters. It preserves all other bits. |
| 191 | ******************************************************************************/ |
| 192 | void cm_write_scr_el3_bit(uint32_t security_state, |
| 193 | uint32_t bit_pos, |
| 194 | uint32_t value) |
| 195 | { |
| 196 | cpu_context_t *ctx; |
| 197 | el3_state_t *state; |
| 198 | uint32_t scr_el3; |
| 199 | |
| 200 | ctx = cm_get_context(read_mpidr(), security_state); |
| 201 | assert(ctx); |
| 202 | |
| 203 | /* Ensure that the bit position is a valid one */ |
| 204 | assert((1 << bit_pos) & SCR_VALID_BIT_MASK); |
| 205 | |
| 206 | /* Ensure that the 'value' is only a bit wide */ |
| 207 | assert(value <= 1); |
| 208 | |
| 209 | /* |
| 210 | * Get the SCR_EL3 value from the cpu context, clear the desired bit |
| 211 | * and set it to its new value. |
| 212 | */ |
| 213 | state = get_el3state_ctx(ctx); |
| 214 | scr_el3 = read_ctx_reg(state, CTX_SCR_EL3); |
| 215 | scr_el3 &= ~(1 << bit_pos); |
| 216 | scr_el3 |= value << bit_pos; |
| 217 | write_ctx_reg(state, CTX_SCR_EL3, scr_el3); |
| 218 | } |
| 219 | |
| 220 | /******************************************************************************* |
| 221 | * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the |
| 222 | * given security state. |
| 223 | ******************************************************************************/ |
| 224 | uint32_t cm_get_scr_el3(uint32_t security_state) |
| 225 | { |
| 226 | cpu_context_t *ctx; |
| 227 | el3_state_t *state; |
| 228 | |
| 229 | ctx = cm_get_context(read_mpidr(), security_state); |
| 230 | assert(ctx); |
| 231 | |
| 232 | /* Populate EL3 state so that ERET jumps to the correct entry */ |
| 233 | state = get_el3state_ctx(ctx); |
| 234 | return read_ctx_reg(state, CTX_SCR_EL3); |
| 235 | } |
| 236 | |
| 237 | /******************************************************************************* |
| Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 238 | * This function is used to program the context that's used for exception |
| 239 | * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for |
| 240 | * the required security state |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 241 | ******************************************************************************/ |
| 242 | void cm_set_next_eret_context(uint32_t security_state) |
| 243 | { |
| Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 244 | cpu_context_t *ctx; |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 245 | #if DEBUG |
| 246 | uint64_t sp_mode; |
| 247 | #endif |
| Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 248 | |
| Achin Gupta | 7aea908 | 2014-02-01 07:51:28 +0000 | [diff] [blame] | 249 | ctx = cm_get_context(read_mpidr(), security_state); |
| 250 | assert(ctx); |
| 251 | |
| 252 | #if DEBUG |
| 253 | /* |
| 254 | * Check that this function is called with SP_EL0 as the stack |
| 255 | * pointer |
| 256 | */ |
| 257 | __asm__ volatile("mrs %0, SPSel\n" |
| 258 | : "=r" (sp_mode)); |
| 259 | |
| 260 | assert(sp_mode == MODE_SP_EL0); |
| 261 | #endif |
| 262 | |
| 263 | __asm__ volatile("msr spsel, #1\n" |
| 264 | "mov sp, %0\n" |
| 265 | "msr spsel, #0\n" |
| 266 | : : "r" (ctx)); |
| 267 | } |
| Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 268 | |
| Soby Mathew | a43d431 | 2014-04-07 15:28:55 +0100 | [diff] [blame] | 269 | /************************************************************************ |
| 270 | * The following function is used to populate the per cpu pointer cache. |
| 271 | * The pointer will be stored in the tpidr_el3 register. |
| 272 | *************************************************************************/ |
| 273 | void cm_init_pcpu_ptr_cache() |
| Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 274 | { |
| Soby Mathew | a43d431 | 2014-04-07 15:28:55 +0100 | [diff] [blame] | 275 | unsigned long mpidr = read_mpidr(); |
| 276 | uint32_t linear_id = platform_get_core_pos(mpidr); |
| 277 | per_cpu_ptr_cache_t *pcpu_ptr_cache; |
| Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 278 | |
| Soby Mathew | a43d431 | 2014-04-07 15:28:55 +0100 | [diff] [blame] | 279 | pcpu_ptr_cache = &per_cpu_ptr_cache_space[linear_id]; |
| 280 | assert(pcpu_ptr_cache); |
| 281 | pcpu_ptr_cache->crash_stack = get_crash_stack(mpidr); |
| Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 282 | |
| Soby Mathew | a43d431 | 2014-04-07 15:28:55 +0100 | [diff] [blame] | 283 | cm_set_pcpu_ptr_cache(pcpu_ptr_cache); |
| Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 284 | } |
| Soby Mathew | a43d431 | 2014-04-07 15:28:55 +0100 | [diff] [blame] | 285 | |
| 286 | |
| 287 | void cm_set_pcpu_ptr_cache(const void *pcpu_ptr) |
| 288 | { |
| 289 | write_tpidr_el3((unsigned long)pcpu_ptr); |
| 290 | } |
| 291 | |
| 292 | void *cm_get_pcpu_ptr_cache(void) |
| 293 | { |
| 294 | return (void *)read_tpidr_el3(); |
| 295 | } |
| 296 | |