blob: aa76f3b68c936eea94ed56bd24a862549301a583 [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
Zelalem Awekef92c0cb2022-01-31 16:59:42 -06002 * Copyright (c) 2013-2022, ARM Limited and Contributors. All rights reserved.
Achin Gupta7aea9082014-02-01 07:51:28 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta7aea9082014-02-01 07:51:28 +00005 */
6
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00007#ifndef CONTEXT_MGMT_H
8#define CONTEXT_MGMT_H
Achin Gupta7aea9082014-02-01 07:51:28 +00009
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000010#include <assert.h>
Antonio Nino Diaz28dce9e2018-05-22 10:09:10 +010011#include <context.h>
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000012#include <stdint.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000013
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <arch.h>
15
Achin Gupta7aea9082014-02-01 07:51:28 +000016/*******************************************************************************
Andrew Thoelke4e126072014-06-04 21:10:52 +010017 * Forward declarations
18 ******************************************************************************/
19struct entry_point_info;
20
21/*******************************************************************************
Achin Gupta7aea9082014-02-01 07:51:28 +000022 * Function & variable prototypes
23 ******************************************************************************/
Dan Handleya17fefa2014-05-14 12:38:32 +010024void cm_init(void);
Soby Mathewb0082d22015-04-09 13:40:55 +010025void *cm_get_context_by_index(unsigned int cpu_idx,
26 unsigned int security_state);
27void cm_set_context_by_index(unsigned int cpu_idx,
28 void *context,
29 unsigned int security_state);
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010030void *cm_get_context(uint32_t security_state);
31void cm_set_context(void *context, uint32_t security_state);
Soby Mathewb0082d22015-04-09 13:40:55 +010032void cm_init_my_context(const struct entry_point_info *ep);
33void cm_init_context_by_index(unsigned int cpu_idx,
34 const struct entry_point_info *ep);
Varun Wadekard1027522020-04-01 09:55:49 -070035void cm_setup_context(cpu_context_t *ctx, const struct entry_point_info *ep);
Andrew Thoelke4e126072014-06-04 21:10:52 +010036void cm_prepare_el3_exit(uint32_t security_state);
Zelalem Awekef92c0cb2022-01-31 16:59:42 -060037void cm_prepare_el3_exit_ns(void);
Soby Mathew748be1d2016-05-05 14:10:46 +010038
Julius Werner8e0ef0f2019-07-09 14:02:43 -070039#ifdef __aarch64__
Boyan Karatotev36cebf92023-03-08 11:56:49 +000040#if IMAGE_BL31
41void cm_manage_extensions_el3(void);
42#endif
Max Shvetsovbdf502d2020-02-25 13:56:19 +000043#if CTX_INCLUDE_EL2_REGS
44void cm_el2_sysregs_context_save(uint32_t security_state);
45void cm_el2_sysregs_context_restore(uint32_t security_state);
46#endif
47
Dan Handleya17fefa2014-05-14 12:38:32 +010048void cm_el1_sysregs_context_save(uint32_t security_state);
49void cm_el1_sysregs_context_restore(uint32_t security_state);
Soby Mathewa0fedc42016-06-16 14:52:04 +010050void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint);
Andrew Thoelke4e126072014-06-04 21:10:52 +010051void cm_set_elr_spsr_el3(uint32_t security_state,
Soby Mathewa0fedc42016-06-16 14:52:04 +010052 uintptr_t entrypoint, uint32_t spsr);
Dan Handleya17fefa2014-05-14 12:38:32 +010053void cm_write_scr_el3_bit(uint32_t security_state,
54 uint32_t bit_pos,
55 uint32_t value);
56void cm_set_next_eret_context(uint32_t security_state);
Louis Mayencourt1c819c32020-01-24 13:30:28 +000057u_register_t cm_get_scr_el3(uint32_t security_state);
Andrew Thoelke8c28fe02014-06-02 11:40:35 +010058
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010059/* Inline definitions */
60
61/*******************************************************************************
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010062 * This function is used to program the context that's used for exception
63 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
64 * the required security state
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010065 ******************************************************************************/
Sandrine Bailleux37a12df2016-04-11 13:17:50 +010066static inline void cm_set_next_context(void *context)
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010067{
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +000068#if ENABLE_ASSERTIONS
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010069 uint64_t sp_mode;
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010070
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010071 /*
72 * Check that this function is called with SP_EL0 as the stack
73 * pointer
74 */
75 __asm__ volatile("mrs %0, SPSel\n"
76 : "=r" (sp_mode));
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010077
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010078 assert(sp_mode == MODE_SP_EL0);
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +000079#endif /* ENABLE_ASSERTIONS */
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010080
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010081 __asm__ volatile("msr spsel, #1\n"
82 "mov sp, %0\n"
83 "msr spsel, #0\n"
84 : : "r" (context));
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010085}
Yatharth Kochar5d361212016-06-28 17:07:09 +010086
87#else
88void *cm_get_next_context(void);
Etienne Carriere662bf932017-06-23 09:37:49 +020089void cm_set_next_context(void *context);
Boyan Karatotev36cebf92023-03-08 11:56:49 +000090static inline void cm_manage_extensions_el3(void) {}
Julius Werner8e0ef0f2019-07-09 14:02:43 -070091#endif /* __aarch64__ */
Yatharth Kochar5d361212016-06-28 17:07:09 +010092
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +000093#endif /* CONTEXT_MGMT_H */