1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * Copyright (c) 2017, Linaro Limited 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #ifndef TEE_TA_MANAGER_H 31 #define TEE_TA_MANAGER_H 32 33 #include <types_ext.h> 34 #include <sys/queue.h> 35 #include <tee_api_types.h> 36 #include <utee_types.h> 37 #include <kernel/tee_common.h> 38 #include <kernel/mutex.h> 39 #include <tee_api_types.h> 40 #include <user_ta_header.h> 41 42 /* Magic TEE identity pointer: set when teecore requests a TA close */ 43 #define KERN_IDENTITY ((TEE_Identity *)-1) 44 /* Operation is initiated by a client (non-secure) app */ 45 #define NSAPP_IDENTITY (NULL) 46 47 TAILQ_HEAD(tee_ta_session_head, tee_ta_session); 48 TAILQ_HEAD(tee_ta_ctx_head, tee_ta_ctx); 49 50 struct mobj; 51 52 struct param_val { 53 uint32_t a; 54 uint32_t b; 55 }; 56 57 struct param_mem { 58 struct mobj *mobj; 59 size_t size; 60 size_t offs; 61 }; 62 63 struct tee_ta_param { 64 uint32_t types; 65 union { 66 struct param_val val; 67 struct param_mem mem; 68 } u[TEE_NUM_PARAMS]; 69 }; 70 71 struct tee_ta_ctx; 72 struct user_ta_ctx; 73 struct pseudo_ta_ctx; 74 75 struct tee_ta_ops { 76 TEE_Result (*enter_open_session)(struct tee_ta_session *s, 77 struct tee_ta_param *param, TEE_ErrorOrigin *eo); 78 TEE_Result (*enter_invoke_cmd)(struct tee_ta_session *s, uint32_t cmd, 79 struct tee_ta_param *param, TEE_ErrorOrigin *eo); 80 void (*enter_close_session)(struct tee_ta_session *s); 81 void (*dump_state)(struct tee_ta_ctx *ctx); 82 void (*destroy)(struct tee_ta_ctx *ctx); 83 uint32_t (*get_instance_id)(struct tee_ta_ctx *ctx); 84 }; 85 86 #if defined(CFG_TA_GPROF_SUPPORT) 87 struct sample_buf { 88 uint32_t nsamples; /* Size of @samples array in uint16_t */ 89 uint32_t offset; /* Passed from user mode */ 90 uint32_t scale; /* Passed from user mode */ 91 uint32_t count; /* Number of samples taken */ 92 bool enabled; /* Sampling enabled? */ 93 uint16_t *samples; 94 uint64_t usr; /* Total user CPU time for this session */ 95 uint64_t usr_entered; /* When this session last entered user mode */ 96 uint32_t freq; /* @usr divided by @freq is in seconds */ 97 }; 98 #endif 99 100 /* Context of a loaded TA */ 101 struct tee_ta_ctx { 102 TEE_UUID uuid; 103 const struct tee_ta_ops *ops; 104 uint32_t flags; /* TA_FLAGS from TA header */ 105 TAILQ_ENTRY(tee_ta_ctx) link; 106 uint32_t panicked; /* True if TA has panicked, written from asm */ 107 uint32_t panic_code; /* Code supplied for panic */ 108 uint32_t ref_count; /* Reference counter for multi session TA */ 109 bool busy; /* context is busy and cannot be entered */ 110 struct condvar busy_cv; /* CV used when context is busy */ 111 }; 112 113 struct tee_ta_session { 114 TAILQ_ENTRY(tee_ta_session) link; 115 TAILQ_ENTRY(tee_ta_session) link_tsd; 116 struct tee_ta_ctx *ctx; /* TA context */ 117 TEE_Identity clnt_id; /* Identify of client */ 118 bool cancel; /* True if TAF is cancelled */ 119 bool cancel_mask; /* True if cancel is masked */ 120 TEE_Time cancel_time; /* Time when to cancel the TAF */ 121 void *user_ctx; /* ??? */ 122 uint32_t ref_count; /* reference counter */ 123 struct condvar refc_cv; /* CV used to wait for ref_count to be 0 */ 124 struct condvar lock_cv; /* CV used to wait for lock */ 125 int lock_thread; /* Id of thread holding the lock */ 126 bool unlink; /* True if session is to be unlinked */ 127 #if defined(CFG_TA_GPROF_SUPPORT) 128 struct sample_buf *sbuf; /* Profiling data (PC sampling) */ 129 #endif 130 }; 131 132 /* Registered contexts */ 133 extern struct tee_ta_ctx_head tee_ctxes; 134 135 extern struct mutex tee_ta_mutex; 136 137 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err, 138 struct tee_ta_session **sess, 139 struct tee_ta_session_head *open_sessions, 140 const TEE_UUID *uuid, 141 const TEE_Identity *clnt_id, 142 uint32_t cancel_req_to, 143 struct tee_ta_param *param); 144 145 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err, 146 struct tee_ta_session *sess, 147 const TEE_Identity *clnt_id, 148 uint32_t cancel_req_to, uint32_t cmd, 149 struct tee_ta_param *param); 150 151 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err, 152 struct tee_ta_session *sess, 153 const TEE_Identity *clnt_id); 154 155 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time); 156 157 /*----------------------------------------------------------------------------- 158 * Function called to close a TA. 159 * Parameters: 160 * id - The session id (in) 161 * Returns: 162 * TEE_Result 163 *---------------------------------------------------------------------------*/ 164 TEE_Result tee_ta_close_session(struct tee_ta_session *sess, 165 struct tee_ta_session_head *open_sessions, 166 const TEE_Identity *clnt_id); 167 168 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess); 169 170 void tee_ta_push_current_session(struct tee_ta_session *sess); 171 struct tee_ta_session *tee_ta_pop_current_session(void); 172 173 struct tee_ta_session *tee_ta_get_calling_session(void); 174 175 TEE_Result tee_ta_get_client_id(TEE_Identity *id); 176 177 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive, 178 struct tee_ta_session_head *open_sessions); 179 180 void tee_ta_put_session(struct tee_ta_session *sess); 181 182 void tee_ta_dump_current(void); 183 184 #if defined(CFG_TA_GPROF_SUPPORT) 185 void tee_ta_gprof_sample_pc(vaddr_t pc); 186 void tee_ta_update_session_utime_suspend(void); 187 void tee_ta_update_session_utime_resume(void); 188 #else 189 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {} 190 static inline void tee_ta_update_session_utime_suspend(void) {} 191 static inline void tee_ta_update_session_utime_resume(void) {} 192 #endif 193 194 #endif 195