1 /* 2 * Copyright (c) 2014, STMicroelectronics International N.V. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef TEE_TA_MANAGER_H 29 #define TEE_TA_MANAGER_H 30 31 #include <types_ext.h> 32 #include <sys/queue.h> 33 #include <tee_api_types.h> 34 #include <utee_types.h> 35 #include <kernel/tee_common.h> 36 #include <kernel/mutex.h> 37 #include <tee_api_types.h> 38 #include <user_ta_header.h> 39 40 /* Magic TEE identity pointer: set when teecore requests a TA close */ 41 #define KERN_IDENTITY ((TEE_Identity *)-1) 42 /* Operation is initiated by a client (non-secure) app */ 43 #define NSAPP_IDENTITY (NULL) 44 45 TAILQ_HEAD(tee_ta_session_head, tee_ta_session); 46 TAILQ_HEAD(tee_ta_ctx_head, tee_ta_ctx); 47 48 struct tee_ta_param { 49 uint32_t types; 50 TEE_Param params[4]; 51 uint32_t param_attr[4]; 52 }; 53 54 struct tee_ta_ctx; 55 struct user_ta_ctx; 56 struct static_ta_ctx; 57 58 struct tee_ta_ops { 59 TEE_Result (*enter_open_session)(struct tee_ta_session *s, 60 struct tee_ta_param *param, TEE_ErrorOrigin *eo); 61 TEE_Result (*enter_invoke_cmd)(struct tee_ta_session *s, uint32_t cmd, 62 struct tee_ta_param *param, TEE_ErrorOrigin *eo); 63 void (*enter_close_session)(struct tee_ta_session *s); 64 void (*dump_state)(struct tee_ta_ctx *ctx); 65 void (*destroy)(struct tee_ta_ctx *ctx); 66 }; 67 68 /* Context of a loaded TA */ 69 struct tee_ta_ctx { 70 TEE_UUID uuid; 71 const struct tee_ta_ops *ops; 72 uint32_t flags; /* TA_FLAGS from TA header */ 73 TAILQ_ENTRY(tee_ta_ctx) link; 74 uint32_t panicked; /* True if TA has panicked, written from asm */ 75 uint32_t panic_code; /* Code supplied for panic */ 76 uint32_t ref_count; /* Reference counter for multi session TA */ 77 bool busy; /* context is busy and cannot be entered */ 78 struct condvar busy_cv; /* CV used when context is busy */ 79 }; 80 81 struct tee_ta_session { 82 TAILQ_ENTRY(tee_ta_session) link; 83 TAILQ_ENTRY(tee_ta_session) link_tsd; 84 struct tee_ta_ctx *ctx; /* TA context */ 85 TEE_Identity clnt_id; /* Identify of client */ 86 bool cancel; /* True if TAF is cancelled */ 87 bool cancel_mask; /* True if cancel is masked */ 88 TEE_Time cancel_time; /* Time when to cancel the TAF */ 89 void *user_ctx; /* ??? */ 90 uint32_t ref_count; /* reference counter */ 91 struct condvar refc_cv; /* CV used to wait for ref_count to be 0 */ 92 struct condvar lock_cv; /* CV used to wait for lock */ 93 int lock_thread; /* Id of thread holding the lock */ 94 bool unlink; /* True if session is to be unlinked */ 95 }; 96 97 /* Registered contexts */ 98 extern struct tee_ta_ctx_head tee_ctxes; 99 100 extern struct mutex tee_ta_mutex; 101 102 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err, 103 struct tee_ta_session **sess, 104 struct tee_ta_session_head *open_sessions, 105 const TEE_UUID *uuid, 106 const TEE_Identity *clnt_id, 107 uint32_t cancel_req_to, 108 struct tee_ta_param *param); 109 110 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err, 111 struct tee_ta_session *sess, 112 const TEE_Identity *clnt_id, 113 uint32_t cancel_req_to, uint32_t cmd, 114 struct tee_ta_param *param); 115 116 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err, 117 struct tee_ta_session *sess, 118 const TEE_Identity *clnt_id); 119 120 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time); 121 122 /*----------------------------------------------------------------------------- 123 * Function called to close a TA. 124 * Parameters: 125 * id - The session id (in) 126 * Returns: 127 * TEE_Result 128 *---------------------------------------------------------------------------*/ 129 TEE_Result tee_ta_close_session(struct tee_ta_session *sess, 130 struct tee_ta_session_head *open_sessions, 131 const TEE_Identity *clnt_id); 132 133 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess); 134 135 void tee_ta_push_current_session(struct tee_ta_session *sess); 136 struct tee_ta_session *tee_ta_pop_current_session(void); 137 138 struct tee_ta_session *tee_ta_get_calling_session(void); 139 140 TEE_Result tee_ta_get_client_id(TEE_Identity *id); 141 142 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive, 143 struct tee_ta_session_head *open_sessions); 144 145 void tee_ta_put_session(struct tee_ta_session *sess); 146 147 void tee_ta_dump_current(void); 148 149 /* 150 * Implemented under core/arch for architecure specific checks 151 */ 152 TEE_Result tee_ta_verify_param(struct tee_ta_session *sess, 153 struct tee_ta_param *param); 154 155 #endif 156