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 }; 111 112 /* Registered contexts */ 113 extern struct tee_ta_ctx_head tee_ctxes; 114 115 extern struct mutex tee_ta_mutex; 116 117 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err, 118 struct tee_ta_session **sess, 119 struct tee_ta_session_head *open_sessions, 120 const TEE_UUID *uuid, 121 const TEE_Identity *clnt_id, 122 uint32_t cancel_req_to, 123 struct tee_ta_param *param); 124 125 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err, 126 struct tee_ta_session *sess, 127 const TEE_Identity *clnt_id, 128 uint32_t cancel_req_to, uint32_t cmd, 129 struct tee_ta_param *param); 130 131 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err, 132 struct tee_ta_session *sess, 133 const TEE_Identity *clnt_id); 134 135 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time); 136 137 /*----------------------------------------------------------------------------- 138 * Function called to close a TA. 139 * Parameters: 140 * id - The session id (in) 141 * Returns: 142 * TEE_Result 143 *---------------------------------------------------------------------------*/ 144 TEE_Result tee_ta_close_session(struct tee_ta_session *sess, 145 struct tee_ta_session_head *open_sessions, 146 const TEE_Identity *clnt_id); 147 148 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess); 149 150 void tee_ta_push_current_session(struct tee_ta_session *sess); 151 struct tee_ta_session *tee_ta_pop_current_session(void); 152 153 struct tee_ta_session *tee_ta_get_calling_session(void); 154 155 struct tee_ta_session *tee_ta_find_session(uint32_t id, 156 struct tee_ta_session_head *open_sessions); 157 158 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive, 159 struct tee_ta_session_head *open_sessions); 160 161 void tee_ta_put_session(struct tee_ta_session *sess); 162 163 #if defined(CFG_TA_GPROF_SUPPORT) 164 void tee_ta_gprof_sample_pc(vaddr_t pc); 165 void tee_ta_update_session_utime_suspend(void); 166 void tee_ta_update_session_utime_resume(void); 167 #else 168 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {} 169 static inline void tee_ta_update_session_utime_suspend(void) {} 170 static inline void tee_ta_update_session_utime_resume(void) {} 171 #endif 172 173 #endif 174