xref: /optee_os/core/include/kernel/tee_ta_manager.h (revision 933a11e07326b0bc485574e5a16ef0990128dfb6)
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 pseudo_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 #if defined(CFG_TA_GPROF_SUPPORT)
84 struct sample_buf {
85 	uint32_t nsamples;	/* Size of @samples array in uint16_t */
86 	uint32_t offset;	/* Passed from user mode */
87 	uint32_t scale;		/* Passed from user mode */
88 	uint32_t count;		/* Number of samples taken */
89 	bool enabled;		/* Sampling enabled? */
90 	uint16_t *samples;
91 	uint64_t usr;		/* Total user CPU time for this session */
92 	uint64_t usr_entered;	/* When this session last entered user mode */
93 	uint32_t freq;		/* @usr divided by @freq is in seconds */
94 };
95 #endif
96 
97 /* Context of a loaded TA */
98 struct tee_ta_ctx {
99 	TEE_UUID uuid;
100 	const struct tee_ta_ops *ops;
101 	uint32_t flags;		/* TA_FLAGS from TA header */
102 	TAILQ_ENTRY(tee_ta_ctx) link;
103 	uint32_t panicked;	/* True if TA has panicked, written from asm */
104 	uint32_t panic_code;	/* Code supplied for panic */
105 	uint32_t ref_count;	/* Reference counter for multi session TA */
106 	bool busy;		/* context is busy and cannot be entered */
107 	struct condvar busy_cv;	/* CV used when context is busy */
108 };
109 
110 struct tee_ta_session {
111 	TAILQ_ENTRY(tee_ta_session) link;
112 	TAILQ_ENTRY(tee_ta_session) link_tsd;
113 	struct tee_ta_ctx *ctx;	/* TA context */
114 	TEE_Identity clnt_id;	/* Identify of client */
115 	bool cancel;		/* True if TAF is cancelled */
116 	bool cancel_mask;	/* True if cancel is masked */
117 	TEE_Time cancel_time;	/* Time when to cancel the TAF */
118 	void *user_ctx;		/* ??? */
119 	uint32_t ref_count;	/* reference counter */
120 	struct condvar refc_cv;	/* CV used to wait for ref_count to be 0 */
121 	struct condvar lock_cv;	/* CV used to wait for lock */
122 	int lock_thread;	/* Id of thread holding the lock */
123 	bool unlink;		/* True if session is to be unlinked */
124 #if defined(CFG_TA_GPROF_SUPPORT)
125 	struct sample_buf *sbuf; /* Profiling data (PC sampling) */
126 #endif
127 };
128 
129 /* Registered contexts */
130 extern struct tee_ta_ctx_head tee_ctxes;
131 
132 extern struct mutex tee_ta_mutex;
133 
134 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
135 			       struct tee_ta_session **sess,
136 			       struct tee_ta_session_head *open_sessions,
137 			       const TEE_UUID *uuid,
138 			       const TEE_Identity *clnt_id,
139 			       uint32_t cancel_req_to,
140 			       struct tee_ta_param *param);
141 
142 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err,
143 				 struct tee_ta_session *sess,
144 				 const TEE_Identity *clnt_id,
145 				 uint32_t cancel_req_to, uint32_t cmd,
146 				 struct tee_ta_param *param);
147 
148 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err,
149 				 struct tee_ta_session *sess,
150 				 const TEE_Identity *clnt_id);
151 
152 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time);
153 
154 /*-----------------------------------------------------------------------------
155  * Function called to close a TA.
156  * Parameters:
157  * id   - The session id (in)
158  * Returns:
159  *        TEE_Result
160  *---------------------------------------------------------------------------*/
161 TEE_Result tee_ta_close_session(struct tee_ta_session *sess,
162 				struct tee_ta_session_head *open_sessions,
163 				const TEE_Identity *clnt_id);
164 
165 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess);
166 
167 void tee_ta_push_current_session(struct tee_ta_session *sess);
168 struct tee_ta_session *tee_ta_pop_current_session(void);
169 
170 struct tee_ta_session *tee_ta_get_calling_session(void);
171 
172 TEE_Result tee_ta_get_client_id(TEE_Identity *id);
173 
174 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
175 			struct tee_ta_session_head *open_sessions);
176 
177 void tee_ta_put_session(struct tee_ta_session *sess);
178 
179 void tee_ta_dump_current(void);
180 
181 #if defined(CFG_TA_GPROF_SUPPORT)
182 void tee_ta_gprof_sample_pc(vaddr_t pc);
183 void tee_ta_update_session_utime_suspend(void);
184 void tee_ta_update_session_utime_resume(void);
185 #else
186 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {}
187 static inline void tee_ta_update_session_utime_suspend(void) {}
188 static inline void tee_ta_update_session_utime_resume(void) {}
189 #endif
190 
191 #endif
192