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