xref: /optee_os/core/include/kernel/tee_ta_manager.h (revision ab2422916fc822d6c8ffc5769cf01d4984113c14)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2017, Linaro Limited
5  */
6 
7 #ifndef TEE_TA_MANAGER_H
8 #define TEE_TA_MANAGER_H
9 
10 #include <assert.h>
11 #include <kernel/mutex.h>
12 #include <kernel/tee_common.h>
13 #include <kernel/ts_manager.h>
14 #include <mm/tee_mmu_types.h>
15 #include <sys/queue.h>
16 #include <tee_api_types.h>
17 #include <types_ext.h>
18 #include <user_ta_header.h>
19 #include <utee_types.h>
20 
21 /* Magic TEE identity pointer: set when teecore requests a TA close */
22 #define KERN_IDENTITY	((TEE_Identity *)-1)
23 /* Operation is initiated by a client (non-secure) app */
24 #define NSAPP_IDENTITY	(NULL)
25 
26 TAILQ_HEAD(tee_ta_session_head, tee_ta_session);
27 TAILQ_HEAD(tee_ta_ctx_head, tee_ta_ctx);
28 
29 struct mobj;
30 
31 struct param_val {
32 	uint32_t a;
33 	uint32_t b;
34 };
35 
36 struct param_mem {
37 	struct mobj *mobj;
38 	size_t size;
39 	size_t offs;
40 };
41 
42 struct tee_ta_param {
43 	uint32_t types;
44 	union {
45 		struct param_val val;
46 		struct param_mem mem;
47 	} u[TEE_NUM_PARAMS];
48 };
49 
50 struct user_ta_ctx;
51 
52 #if defined(CFG_TA_GPROF_SUPPORT)
53 struct sample_buf {
54 	uint32_t nsamples;	/* Size of @samples array in uint16_t */
55 	uint32_t offset;	/* Passed from user mode */
56 	uint32_t scale;		/* Passed from user mode */
57 	uint32_t count;		/* Number of samples taken */
58 	bool enabled;		/* Sampling enabled? */
59 	uint16_t *samples;
60 	uint64_t usr;		/* Total user CPU time for this session */
61 	uint64_t usr_entered;	/* When this session last entered user mode */
62 	uint32_t freq;		/* @usr divided by @freq is in seconds */
63 };
64 #endif
65 
66 /* Context of a loaded TA */
67 struct tee_ta_ctx {
68 	uint32_t flags;		/* TA_FLAGS from TA header */
69 	TAILQ_ENTRY(tee_ta_ctx) link;
70 	struct ts_ctx ts_ctx;
71 	uint32_t panicked;	/* True if TA has panicked, written from asm */
72 	uint32_t panic_code;	/* Code supplied for panic */
73 	uint32_t ref_count;	/* Reference counter for multi session TA */
74 	bool busy;		/* Context is busy and cannot be entered */
75 	bool initializing;	/* Context is initializing */
76 	struct condvar busy_cv;	/* CV used when context is busy */
77 };
78 
79 struct tee_ta_session {
80 	TAILQ_ENTRY(tee_ta_session) link;
81 	struct ts_session ts_sess;
82 	uint32_t id;		/* Session handle (0 is invalid) */
83 	TEE_Identity clnt_id;	/* Identify of client */
84 	bool cancel;		/* True if TA invocation is cancelled */
85 	bool cancel_mask;	/* True if cancel is masked */
86 	TEE_Time cancel_time;	/* Time when to cancel the TA invocation */
87 	void *user_ctx;		/* Opaque session handle assigned by PTA */
88 	uint32_t ref_count;	/* reference counter */
89 	struct condvar refc_cv;	/* CV used to wait for ref_count to be 0 */
90 	struct condvar lock_cv;	/* CV used to wait for lock */
91 	short int lock_thread;	/* Id of thread holding the lock */
92 	bool unlink;		/* True if session is to be unlinked */
93 };
94 
95 /* Registered contexts */
96 extern struct tee_ta_ctx_head tee_ctxes;
97 
98 extern struct mutex tee_ta_mutex;
99 extern struct condvar tee_ta_init_cv;
100 
101 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
102 			       struct tee_ta_session **sess,
103 			       struct tee_ta_session_head *open_sessions,
104 			       const TEE_UUID *uuid,
105 			       const TEE_Identity *clnt_id,
106 			       uint32_t cancel_req_to,
107 			       struct tee_ta_param *param);
108 
109 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err,
110 				 struct tee_ta_session *sess,
111 				 const TEE_Identity *clnt_id,
112 				 uint32_t cancel_req_to, uint32_t cmd,
113 				 struct tee_ta_param *param);
114 
115 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err,
116 				 struct tee_ta_session *sess,
117 				 const TEE_Identity *clnt_id);
118 
119 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time);
120 
121 /*-----------------------------------------------------------------------------
122  * Function called to close a TA.
123  * Parameters:
124  * id   - The session id (in)
125  * Returns:
126  *        TEE_Result
127  *---------------------------------------------------------------------------*/
128 TEE_Result tee_ta_close_session(struct tee_ta_session *sess,
129 				struct tee_ta_session_head *open_sessions,
130 				const TEE_Identity *clnt_id);
131 
132 
133 
134 struct tee_ta_session *tee_ta_find_session(uint32_t id,
135 			struct tee_ta_session_head *open_sessions);
136 
137 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
138 			struct tee_ta_session_head *open_sessions);
139 
140 void tee_ta_put_session(struct tee_ta_session *sess);
141 
142 #if defined(CFG_TA_GPROF_SUPPORT)
143 void tee_ta_update_session_utime_suspend(void);
144 void tee_ta_update_session_utime_resume(void);
145 void tee_ta_gprof_sample_pc(vaddr_t pc);
146 #else
147 static inline void tee_ta_update_session_utime_suspend(void) {}
148 static inline void tee_ta_update_session_utime_resume(void) {}
149 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {}
150 #endif
151 #if defined(CFG_FTRACE_SUPPORT)
152 void tee_ta_ftrace_update_times_suspend(void);
153 void tee_ta_ftrace_update_times_resume(void);
154 #else
155 static inline void tee_ta_ftrace_update_times_suspend(void) {}
156 static inline void tee_ta_ftrace_update_times_resume(void) {}
157 #endif
158 
159 bool is_ta_ctx(struct ts_ctx *ctx);
160 
161 static inline struct tee_ta_session * __noprof
162 to_ta_session(struct ts_session *sess)
163 {
164 	assert(is_ta_ctx(sess->ctx));
165 	return container_of(sess, struct tee_ta_session, ts_sess);
166 }
167 
168 static inline struct tee_ta_ctx *to_ta_ctx(struct ts_ctx *ctx)
169 {
170 	assert(is_ta_ctx(ctx));
171 	return container_of(ctx, struct tee_ta_ctx, ts_ctx);
172 }
173 #endif
174