1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2020, Linaro Limited 5 */ 6 7 #include <kernel/panic.h> 8 #include <kernel/pseudo_ta.h> 9 #include <kernel/tee_ta_manager.h> 10 #include <kernel/thread.h> 11 #include <kernel/user_mode_ctx.h> 12 #include <mm/core_mmu.h> 13 #include <mm/tee_mmu.h> 14 15 static void update_current_ctx(struct thread_specific_data *tsd) 16 { 17 struct tee_ta_ctx *ctx = NULL; 18 struct ts_session *s = TAILQ_FIRST(&tsd->sess_stack); 19 20 if (s) { 21 if (is_pseudo_ta_ctx(s->ctx)) 22 s = TAILQ_NEXT(s, link_tsd); 23 24 if (s) 25 ctx = s->ctx; 26 } 27 28 if (tsd->ctx != ctx) 29 tee_mmu_set_ctx(ctx); 30 /* 31 * If current context is of user mode, then it has to be active too. 32 */ 33 if (is_user_mode_ctx(ctx) != core_mmu_user_mapping_is_active()) 34 panic("unexpected active mapping"); 35 } 36 37 void ts_push_current_session(struct ts_session *s) 38 { 39 struct thread_specific_data *tsd = thread_get_tsd(); 40 41 TAILQ_INSERT_HEAD(&tsd->sess_stack, s, link_tsd); 42 update_current_ctx(tsd); 43 } 44 45 struct ts_session *ts_pop_current_session(void) 46 { 47 struct thread_specific_data *tsd = thread_get_tsd(); 48 struct ts_session *s = TAILQ_FIRST(&tsd->sess_stack); 49 50 if (s) { 51 TAILQ_REMOVE(&tsd->sess_stack, s, link_tsd); 52 update_current_ctx(tsd); 53 } 54 return s; 55 } 56 57 struct ts_session *ts_get_calling_session(void) 58 { 59 struct ts_session *s = ts_get_current_session(); 60 61 if (s) 62 s = TAILQ_NEXT(s, link_tsd); 63 return s; 64 } 65 66 struct ts_session *ts_get_current_session_may_fail(void) 67 { 68 return TAILQ_FIRST(&thread_get_tsd()->sess_stack); 69 } 70 71 struct ts_session *ts_get_current_session(void) 72 { 73 struct ts_session *s = ts_get_current_session_may_fail(); 74 75 if (!s) 76 panic(); 77 return s; 78 } 79