1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2017, Linaro Limited 5 */ 6 7 #ifndef TEE_TA_MANAGER_H 8 #define TEE_TA_MANAGER_H 9 10 #include <kernel/mutex.h> 11 #include <kernel/tee_common.h> 12 #include <mm/tee_mmu_types.h> 13 #include <sys/queue.h> 14 #include <tee_api_types.h> 15 #include <tee_api_types.h> 16 #include <types_ext.h> 17 #include <user_ta_header.h> 18 #include <utee_types.h> 19 20 /* Magic TEE identity pointer: set when teecore requests a TA close */ 21 #define KERN_IDENTITY ((TEE_Identity *)-1) 22 /* Operation is initiated by a client (non-secure) app */ 23 #define NSAPP_IDENTITY (NULL) 24 25 TAILQ_HEAD(tee_ta_session_head, tee_ta_session); 26 TAILQ_HEAD(tee_ta_ctx_head, tee_ta_ctx); 27 28 struct mobj; 29 30 struct param_val { 31 uint32_t a; 32 uint32_t b; 33 }; 34 35 struct param_mem { 36 struct mobj *mobj; 37 size_t size; 38 size_t offs; 39 }; 40 41 struct tee_ta_param { 42 uint32_t types; 43 union { 44 struct param_val val; 45 struct param_mem mem; 46 } u[TEE_NUM_PARAMS]; 47 }; 48 49 struct tee_ta_ctx; 50 struct user_ta_ctx; 51 struct pseudo_ta_ctx; 52 53 struct tee_ta_ops { 54 TEE_Result (*enter_open_session)(struct tee_ta_session *s, 55 struct tee_ta_param *param, TEE_ErrorOrigin *eo); 56 TEE_Result (*enter_invoke_cmd)(struct tee_ta_session *s, uint32_t cmd, 57 struct tee_ta_param *param, TEE_ErrorOrigin *eo); 58 void (*enter_close_session)(struct tee_ta_session *s); 59 void (*dump_state)(struct tee_ta_ctx *ctx); 60 void (*dump_ftrace)(struct tee_ta_ctx *ctx); 61 void (*destroy)(struct tee_ta_ctx *ctx); 62 uint32_t (*get_instance_id)(struct tee_ta_ctx *ctx); 63 }; 64 65 #if defined(CFG_TA_GPROF_SUPPORT) 66 struct sample_buf { 67 uint32_t nsamples; /* Size of @samples array in uint16_t */ 68 uint32_t offset; /* Passed from user mode */ 69 uint32_t scale; /* Passed from user mode */ 70 uint32_t count; /* Number of samples taken */ 71 bool enabled; /* Sampling enabled? */ 72 uint16_t *samples; 73 uint64_t usr; /* Total user CPU time for this session */ 74 uint64_t usr_entered; /* When this session last entered user mode */ 75 uint32_t freq; /* @usr divided by @freq is in seconds */ 76 }; 77 #endif 78 79 /* Context of a loaded TA */ 80 struct tee_ta_ctx { 81 TEE_UUID uuid; 82 const struct tee_ta_ops *ops; 83 uint32_t flags; /* TA_FLAGS from TA header */ 84 TAILQ_ENTRY(tee_ta_ctx) link; 85 uint32_t panicked; /* True if TA has panicked, written from asm */ 86 uint32_t panic_code; /* Code supplied for panic */ 87 uint32_t ref_count; /* Reference counter for multi session TA */ 88 bool busy; /* Context is busy and cannot be entered */ 89 bool initializing; /* Context is initializing */ 90 struct condvar busy_cv; /* CV used when context is busy */ 91 }; 92 93 struct tee_ta_session { 94 TAILQ_ENTRY(tee_ta_session) link; 95 TAILQ_ENTRY(tee_ta_session) link_tsd; 96 uint32_t id; /* Session handle (0 is invalid) */ 97 struct tee_ta_ctx *ctx; /* TA context */ 98 TEE_Identity clnt_id; /* Identify of client */ 99 bool cancel; /* True if TA invocation is cancelled */ 100 bool cancel_mask; /* True if cancel is masked */ 101 TEE_Time cancel_time; /* Time when to cancel the TA invocation */ 102 void *user_ctx; /* Opaque session handle assigned by PTA */ 103 uint32_t ref_count; /* reference counter */ 104 struct condvar refc_cv; /* CV used to wait for ref_count to be 0 */ 105 struct condvar lock_cv; /* CV used to wait for lock */ 106 int lock_thread; /* Id of thread holding the lock */ 107 bool unlink; /* True if session is to be unlinked */ 108 #if defined(CFG_TA_GPROF_SUPPORT) 109 struct sample_buf *sbuf; /* Profiling data (PC sampling) */ 110 #endif 111 #if defined(CFG_FTRACE_SUPPORT) 112 struct ftrace_buf *fbuf; /* ftrace buffer */ 113 #endif 114 }; 115 116 /* Registered contexts */ 117 extern struct tee_ta_ctx_head tee_ctxes; 118 119 extern struct mutex tee_ta_mutex; 120 121 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err, 122 struct tee_ta_session **sess, 123 struct tee_ta_session_head *open_sessions, 124 const TEE_UUID *uuid, 125 const TEE_Identity *clnt_id, 126 uint32_t cancel_req_to, 127 struct tee_ta_param *param); 128 129 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err, 130 struct tee_ta_session *sess, 131 const TEE_Identity *clnt_id, 132 uint32_t cancel_req_to, uint32_t cmd, 133 struct tee_ta_param *param); 134 135 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err, 136 struct tee_ta_session *sess, 137 const TEE_Identity *clnt_id); 138 139 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time); 140 141 /*----------------------------------------------------------------------------- 142 * Function called to close a TA. 143 * Parameters: 144 * id - The session id (in) 145 * Returns: 146 * TEE_Result 147 *---------------------------------------------------------------------------*/ 148 TEE_Result tee_ta_close_session(struct tee_ta_session *sess, 149 struct tee_ta_session_head *open_sessions, 150 const TEE_Identity *clnt_id); 151 152 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess); 153 154 void tee_ta_push_current_session(struct tee_ta_session *sess); 155 struct tee_ta_session *tee_ta_pop_current_session(void); 156 157 struct tee_ta_session *tee_ta_get_calling_session(void); 158 159 struct tee_ta_session *tee_ta_find_session(uint32_t id, 160 struct tee_ta_session_head *open_sessions); 161 162 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive, 163 struct tee_ta_session_head *open_sessions); 164 165 void tee_ta_put_session(struct tee_ta_session *sess); 166 167 #if defined(CFG_TA_GPROF_SUPPORT) 168 void tee_ta_update_session_utime_suspend(void); 169 void tee_ta_update_session_utime_resume(void); 170 void tee_ta_gprof_sample_pc(vaddr_t pc); 171 #else 172 static inline void tee_ta_update_session_utime_suspend(void) {} 173 static inline void tee_ta_update_session_utime_resume(void) {} 174 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {} 175 #endif 176 #if defined(CFG_FTRACE_SUPPORT) 177 void tee_ta_ftrace_update_times_suspend(void); 178 void tee_ta_ftrace_update_times_resume(void); 179 #else 180 static inline void tee_ta_ftrace_update_times_suspend(void) {} 181 static inline void tee_ta_ftrace_update_times_resume(void) {} 182 #endif 183 184 #endif 185