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