1532ed618SSoby Mathew /* 2*a0fee747SAntonio Nino Diaz * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5532ed618SSoby Mathew */ 6532ed618SSoby Mathew 7*a0fee747SAntonio Nino Diaz #ifndef CONTEXT_MGMT_H 8*a0fee747SAntonio Nino Diaz #define CONTEXT_MGMT_H 9532ed618SSoby Mathew 10532ed618SSoby Mathew #include <arch.h> 11b10d4499SJeenu Viswambharan #include <assert.h> 121634cae8SAntonio Nino Diaz #include <context.h> 13b10d4499SJeenu Viswambharan #include <stdint.h> 14532ed618SSoby Mathew 15532ed618SSoby Mathew /******************************************************************************* 16532ed618SSoby Mathew * Forward declarations 17532ed618SSoby Mathew ******************************************************************************/ 18532ed618SSoby Mathew struct entry_point_info; 19532ed618SSoby Mathew 20532ed618SSoby Mathew /******************************************************************************* 21532ed618SSoby Mathew * Function & variable prototypes 22532ed618SSoby Mathew ******************************************************************************/ 23532ed618SSoby Mathew void cm_init(void); 24532ed618SSoby Mathew void *cm_get_context_by_index(unsigned int cpu_idx, 25532ed618SSoby Mathew unsigned int security_state); 26532ed618SSoby Mathew void cm_set_context_by_index(unsigned int cpu_idx, 27532ed618SSoby Mathew void *context, 28532ed618SSoby Mathew unsigned int security_state); 29532ed618SSoby Mathew void *cm_get_context(uint32_t security_state); 30532ed618SSoby Mathew void cm_set_context(void *context, uint32_t security_state); 31532ed618SSoby Mathew void cm_init_my_context(const struct entry_point_info *ep); 32532ed618SSoby Mathew void cm_init_context_by_index(unsigned int cpu_idx, 33532ed618SSoby Mathew const struct entry_point_info *ep); 341634cae8SAntonio Nino Diaz void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep); 35532ed618SSoby Mathew void cm_prepare_el3_exit(uint32_t security_state); 36e33b78a6SSoby Mathew 37e33b78a6SSoby Mathew #ifndef AARCH32 38532ed618SSoby Mathew void cm_el1_sysregs_context_save(uint32_t security_state); 39532ed618SSoby Mathew void cm_el1_sysregs_context_restore(uint32_t security_state); 40532ed618SSoby Mathew void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint); 41532ed618SSoby Mathew void cm_set_elr_spsr_el3(uint32_t security_state, 42532ed618SSoby Mathew uintptr_t entrypoint, uint32_t spsr); 43532ed618SSoby Mathew void cm_write_scr_el3_bit(uint32_t security_state, 44532ed618SSoby Mathew uint32_t bit_pos, 45532ed618SSoby Mathew uint32_t value); 46532ed618SSoby Mathew void cm_set_next_eret_context(uint32_t security_state); 47532ed618SSoby Mathew uint32_t cm_get_scr_el3(uint32_t security_state); 48532ed618SSoby Mathew 49532ed618SSoby Mathew /* Inline definitions */ 50532ed618SSoby Mathew 51532ed618SSoby Mathew /******************************************************************************* 52532ed618SSoby Mathew * This function is used to program the context that's used for exception 53532ed618SSoby Mathew * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for 54532ed618SSoby Mathew * the required security state 55532ed618SSoby Mathew ******************************************************************************/ 56532ed618SSoby Mathew static inline void cm_set_next_context(void *context) 57532ed618SSoby Mathew { 58aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS 59532ed618SSoby Mathew uint64_t sp_mode; 60532ed618SSoby Mathew 61532ed618SSoby Mathew /* 62532ed618SSoby Mathew * Check that this function is called with SP_EL0 as the stack 63532ed618SSoby Mathew * pointer 64532ed618SSoby Mathew */ 65532ed618SSoby Mathew __asm__ volatile("mrs %0, SPSel\n" 66532ed618SSoby Mathew : "=r" (sp_mode)); 67532ed618SSoby Mathew 68532ed618SSoby Mathew assert(sp_mode == MODE_SP_EL0); 69aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */ 70532ed618SSoby Mathew 71532ed618SSoby Mathew __asm__ volatile("msr spsel, #1\n" 72532ed618SSoby Mathew "mov sp, %0\n" 73532ed618SSoby Mathew "msr spsel, #0\n" 74532ed618SSoby Mathew : : "r" (context)); 75532ed618SSoby Mathew } 76f3b4914bSYatharth Kochar 77f3b4914bSYatharth Kochar #else 78f3b4914bSYatharth Kochar void *cm_get_next_context(void); 792ed7b71eSEtienne Carriere void cm_set_next_context(void *context); 80e33b78a6SSoby Mathew #endif /* AARCH32 */ 81f3b4914bSYatharth Kochar 82*a0fee747SAntonio Nino Diaz #endif /* CONTEXT_MGMT_H */ 83