blob: 70dbd463e14c19952c5f3d7411fc9115023b3f94 [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
Chris Kaya6b36432024-02-06 15:43:40 +00002 * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
Achin Gupta7aea9082014-02-01 07:51:28 +00003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta7aea9082014-02-01 07:51:28 +00005 */
6
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00007#ifndef CONTEXT_MGMT_H
8#define CONTEXT_MGMT_H
Achin Gupta7aea9082014-02-01 07:51:28 +00009
Jeenu Viswambharanb10d4492017-02-16 14:55:15 +000010#include <assert.h>
Antonio Nino Diaz1634cae2018-05-22 10:09:10 +010011#include <context.h>
Jeenu Viswambharanb10d4492017-02-16 14:55:15 +000012#include <stdint.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000013
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000014#include <arch.h>
15
Achin Gupta7aea9082014-02-01 07:51:28 +000016/*******************************************************************************
Andrew Thoelke167a9352014-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 Handleyc6bc0712014-05-14 12:38:32 +010024void cm_init(void);
Soby Mathew12d0d002015-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 Kocharbbf8f6f2015-10-02 17:56:48 +010030void *cm_get_context(uint32_t security_state);
31void cm_set_context(void *context, uint32_t security_state);
Soby Mathew12d0d002015-04-09 13:40:55 +010032void cm_init_my_context(const struct entry_point_info *ep);
Varun Wadekar25d819a2020-04-01 09:55:49 -070033void cm_setup_context(cpu_context_t *ctx, const struct entry_point_info *ep);
Andrew Thoelke167a9352014-06-04 21:10:52 +010034void cm_prepare_el3_exit(uint32_t security_state);
Zelalem Aweke8b95e842022-01-31 16:59:42 -060035void cm_prepare_el3_exit_ns(void);
Soby Mathewe33b78a2016-05-05 14:10:46 +010036
Chris Kaya6b36432024-02-06 15:43:40 +000037#if !IMAGE_BL1
38void cm_init_context_by_index(unsigned int cpu_idx,
39 const struct entry_point_info *ep);
40#endif /* !IMAGE_BL1 */
41
Julius Werner402b3cf2019-07-09 14:02:43 -070042#ifdef __aarch64__
Boyan Karatotev24a70732023-03-08 11:56:49 +000043#if IMAGE_BL31
44void cm_manage_extensions_el3(void);
Elizabeth Ho461c0a52023-07-18 14:10:25 +010045void manage_extensions_nonsecure_per_world(void);
Jayanth Dodderi Chidanand4087ed62023-12-11 11:22:02 +000046void cm_el3_arch_init_per_world(per_world_context_t *per_world_ctx);
Manish Pandey2f41c9a2024-07-12 12:40:04 +010047void cm_handle_asymmetric_features(void);
Boyan Karatotev24a70732023-03-08 11:56:49 +000048#endif
Jayanth Dodderi Chidanand4087ed62023-12-11 11:22:02 +000049
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010050#if (CTX_INCLUDE_EL2_REGS && IMAGE_BL31)
Max Shvetsov28f39f02020-02-25 13:56:19 +000051void cm_el2_sysregs_context_save(uint32_t security_state);
52void cm_el2_sysregs_context_restore(uint32_t security_state);
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010053#else
Dan Handleyc6bc0712014-05-14 12:38:32 +010054void cm_el1_sysregs_context_save(uint32_t security_state);
55void cm_el1_sysregs_context_restore(uint32_t security_state);
Jayanth Dodderi Chidananda0674ab2024-05-07 18:50:57 +010056#endif /* (CTX_INCLUDE_EL2_REGS && IMAGE_BL31) */
57
Soby Mathew4c0d0392016-06-16 14:52:04 +010058void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint);
Andrew Thoelke167a9352014-06-04 21:10:52 +010059void cm_set_elr_spsr_el3(uint32_t security_state,
Soby Mathew4c0d0392016-06-16 14:52:04 +010060 uintptr_t entrypoint, uint32_t spsr);
Dan Handleyc6bc0712014-05-14 12:38:32 +010061void cm_write_scr_el3_bit(uint32_t security_state,
62 uint32_t bit_pos,
63 uint32_t value);
64void cm_set_next_eret_context(uint32_t security_state);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +000065u_register_t cm_get_scr_el3(uint32_t security_state);
Andrew Thoelke5e910072014-06-02 11:40:35 +010066
Andrew Thoelkeaaba4f22014-06-02 10:00:25 +010067/* Inline definitions */
68
69/*******************************************************************************
Yatharth Kocharbbf8f6f2015-10-02 17:56:48 +010070 * This function is used to program the context that's used for exception
71 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
72 * the required security state
Andrew Thoelkeaaba4f22014-06-02 10:00:25 +010073 ******************************************************************************/
Sandrine Bailleux10c252c2016-04-11 13:17:50 +010074static inline void cm_set_next_context(void *context)
Andrew Thoelkeaaba4f22014-06-02 10:00:25 +010075{
Antonio Nino Diazaa613682017-03-22 15:48:51 +000076#if ENABLE_ASSERTIONS
Yatharth Kocharbbf8f6f2015-10-02 17:56:48 +010077 uint64_t sp_mode;
Andrew Thoelkeaaba4f22014-06-02 10:00:25 +010078
Yatharth Kocharbbf8f6f2015-10-02 17:56:48 +010079 /*
80 * Check that this function is called with SP_EL0 as the stack
81 * pointer
82 */
83 __asm__ volatile("mrs %0, SPSel\n"
84 : "=r" (sp_mode));
85
86 assert(sp_mode == MODE_SP_EL0);
Antonio Nino Diazaa613682017-03-22 15:48:51 +000087#endif /* ENABLE_ASSERTIONS */
Yatharth Kocharbbf8f6f2015-10-02 17:56:48 +010088
89 __asm__ volatile("msr spsel, #1\n"
90 "mov sp, %0\n"
91 "msr spsel, #0\n"
92 : : "r" (context));
Andrew Thoelkeaaba4f22014-06-02 10:00:25 +010093}
Yatharth Kocharf3b49142016-06-28 17:07:09 +010094
95#else
96void *cm_get_next_context(void);
Etienne Carriere2ed7b712017-06-23 09:37:49 +020097void cm_set_next_context(void *context);
Boyan Karatotev24a70732023-03-08 11:56:49 +000098static inline void cm_manage_extensions_el3(void) {}
Elizabeth Ho461c0a52023-07-18 14:10:25 +010099static inline void manage_extensions_nonsecure_per_world(void) {}
Manish Pandey2f41c9a2024-07-12 12:40:04 +0100100static inline void cm_handle_asymmetric_features(void) {}
Julius Werner402b3cf2019-07-09 14:02:43 -0700101#endif /* __aarch64__ */
Yatharth Kocharf3b49142016-06-28 17:07:09 +0100102
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000103#endif /* CONTEXT_MGMT_H */