xref: /optee_os/core/include/kernel/tee_ta_manager.h (revision 0dcfa5686d022738f9e6372b19e6b30851523dd0)
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 mobj;
49 
50 struct param_val {
51 	uint32_t a;
52 	uint32_t b;
53 };
54 
55 struct param_mem {
56 	struct mobj *mobj;
57 	size_t size;
58 	size_t offs;
59 };
60 
61 struct tee_ta_param {
62 	uint32_t types;
63 	union {
64 		struct param_val val;
65 		struct param_mem mem;
66 	} u[TEE_NUM_PARAMS];
67 };
68 
69 struct tee_ta_ctx;
70 struct user_ta_ctx;
71 struct static_ta_ctx;
72 
73 struct tee_ta_ops {
74 	TEE_Result (*enter_open_session)(struct tee_ta_session *s,
75 			struct tee_ta_param *param, TEE_ErrorOrigin *eo);
76 	TEE_Result (*enter_invoke_cmd)(struct tee_ta_session *s, uint32_t cmd,
77 			struct tee_ta_param *param, TEE_ErrorOrigin *eo);
78 	void (*enter_close_session)(struct tee_ta_session *s);
79 	void (*dump_state)(struct tee_ta_ctx *ctx);
80 	void (*destroy)(struct tee_ta_ctx *ctx);
81 };
82 
83 /* Context of a loaded TA */
84 struct tee_ta_ctx {
85 	TEE_UUID uuid;
86 	const struct tee_ta_ops *ops;
87 	uint32_t flags;		/* TA_FLAGS from TA header */
88 	TAILQ_ENTRY(tee_ta_ctx) link;
89 	uint32_t panicked;	/* True if TA has panicked, written from asm */
90 	uint32_t panic_code;	/* Code supplied for panic */
91 	uint32_t ref_count;	/* Reference counter for multi session TA */
92 	bool busy;		/* context is busy and cannot be entered */
93 	struct condvar busy_cv;	/* CV used when context is busy */
94 };
95 
96 struct tee_ta_session {
97 	TAILQ_ENTRY(tee_ta_session) link;
98 	TAILQ_ENTRY(tee_ta_session) link_tsd;
99 	struct tee_ta_ctx *ctx;	/* TA context */
100 	TEE_Identity clnt_id;	/* Identify of client */
101 	bool cancel;		/* True if TAF is cancelled */
102 	bool cancel_mask;	/* True if cancel is masked */
103 	TEE_Time cancel_time;	/* Time when to cancel the TAF */
104 	void *user_ctx;		/* ??? */
105 	uint32_t ref_count;	/* reference counter */
106 	struct condvar refc_cv;	/* CV used to wait for ref_count to be 0 */
107 	struct condvar lock_cv;	/* CV used to wait for lock */
108 	int lock_thread;	/* Id of thread holding the lock */
109 	bool unlink;		/* True if session is to be unlinked */
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 TEE_Result tee_ta_get_client_id(TEE_Identity *id);
156 
157 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
158 			struct tee_ta_session_head *open_sessions);
159 
160 void tee_ta_put_session(struct tee_ta_session *sess);
161 
162 void tee_ta_dump_current(void);
163 
164 /*
165  * Implemented under core/arch for architecure specific checks
166  */
167 TEE_Result tee_ta_verify_param(struct tee_ta_session *sess,
168 			       struct tee_ta_param *param);
169 
170 #endif
171