1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2017, Linaro Limited
5  * Copyright (c) 2020, Arm Limited
6  */
7 
8 #ifndef TEE_TA_MANAGER_H
9 #define TEE_TA_MANAGER_H
10 
11 #include <assert.h>
12 #include <kernel/mutex.h>
13 #include <kernel/tee_common.h>
14 #include <kernel/ts_manager.h>
15 #include <mm/tee_mmu_types.h>
16 #include <sys/queue.h>
17 #include <tee_api_types.h>
18 #include <types_ext.h>
19 #include <user_ta_header.h>
20 #include <utee_types.h>
21 
22 /* Magic TEE identity pointer: set when teecore requests a TA close */
23 #define KERN_IDENTITY	((TEE_Identity *)-1)
24 /* Operation is initiated by a client (non-secure) app */
25 #define NSAPP_IDENTITY	(NULL)
26 
27 TAILQ_HEAD(tee_ta_session_head, tee_ta_session);
28 TAILQ_HEAD(tee_ta_ctx_head, tee_ta_ctx);
29 
30 struct mobj;
31 
32 struct param_val {
33 	uint32_t a;
34 	uint32_t b;
35 };
36 
37 struct param_mem {
38 	struct mobj *mobj;
39 	size_t size;
40 	size_t offs;
41 };
42 
43 struct tee_ta_param {
44 	uint32_t types;
45 	union {
46 		struct param_val val;
47 		struct param_mem mem;
48 	} u[TEE_NUM_PARAMS];
49 };
50 
51 struct user_ta_ctx;
52 
53 #if defined(CFG_TA_GPROF_SUPPORT)
54 struct sample_buf {
55 	uint32_t nsamples;	/* Size of @samples array in uint16_t */
56 	uint32_t offset;	/* Passed from user mode */
57 	uint32_t scale;		/* Passed from user mode */
58 	uint32_t count;		/* Number of samples taken */
59 	bool enabled;		/* Sampling enabled? */
60 	uint16_t *samples;
61 	uint64_t usr;		/* Total user CPU time for this session */
62 	uint64_t usr_entered;	/* When this session last entered user mode */
63 	uint32_t freq;		/* @usr divided by @freq is in seconds */
64 };
65 #endif
66 
67 /* Context of a loaded TA */
68 struct tee_ta_ctx {
69 	uint32_t flags;		/* TA_FLAGS from TA header */
70 	TAILQ_ENTRY(tee_ta_ctx) link;
71 	struct ts_ctx ts_ctx;
72 	uint32_t panicked;	/* True if TA has panicked, written from asm */
73 	uint32_t panic_code;	/* Code supplied for panic */
74 	uint32_t ref_count;	/* Reference counter for multi session TA */
75 	bool busy;		/* Context is busy and cannot be entered */
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 	struct tee_ta_param *param;
85 	TEE_ErrorOrigin err_origin;
86 	bool cancel;		/* True if TA invocation is cancelled */
87 	bool cancel_mask;	/* True if cancel is masked */
88 	TEE_Time cancel_time;	/* Time when to cancel the TA invocation */
89 	uint32_t ref_count;	/* reference counter */
90 	struct condvar refc_cv;	/* CV used to wait for ref_count to be 0 */
91 	struct condvar lock_cv;	/* CV used to wait for lock */
92 	short int lock_thread;	/* Id of thread holding the lock */
93 	bool unlink;		/* True if session is to be unlinked */
94 };
95 
96 /* Registered contexts */
97 extern struct tee_ta_ctx_head tee_ctxes;
98 
99 extern struct mutex tee_ta_mutex;
100 extern struct condvar tee_ta_init_cv;
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 
134 
135 struct tee_ta_session *tee_ta_find_session(uint32_t id,
136 			struct tee_ta_session_head *open_sessions);
137 
138 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
139 			struct tee_ta_session_head *open_sessions);
140 
141 void tee_ta_put_session(struct tee_ta_session *sess);
142 
143 #if defined(CFG_TA_GPROF_SUPPORT)
144 void tee_ta_update_session_utime_suspend(void);
145 void tee_ta_update_session_utime_resume(void);
146 void tee_ta_gprof_sample_pc(vaddr_t pc);
147 #else
tee_ta_update_session_utime_suspend(void)148 static inline void tee_ta_update_session_utime_suspend(void) {}
tee_ta_update_session_utime_resume(void)149 static inline void tee_ta_update_session_utime_resume(void) {}
tee_ta_gprof_sample_pc(vaddr_t pc __unused)150 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {}
151 #endif
152 #if defined(CFG_FTRACE_SUPPORT)
153 void tee_ta_ftrace_update_times_suspend(void);
154 void tee_ta_ftrace_update_times_resume(void);
155 #else
tee_ta_ftrace_update_times_suspend(void)156 static inline void tee_ta_ftrace_update_times_suspend(void) {}
tee_ta_ftrace_update_times_resume(void)157 static inline void tee_ta_ftrace_update_times_resume(void) {}
158 #endif
159 
160 bool is_ta_ctx(struct ts_ctx *ctx);
161 
162 struct tee_ta_session *to_ta_session(struct ts_session *sess);
163 
to_ta_ctx(struct ts_ctx * ctx)164 static inline struct tee_ta_ctx *to_ta_ctx(struct ts_ctx *ctx)
165 {
166 	assert(is_ta_ctx(ctx));
167 	return container_of(ctx, struct tee_ta_ctx, ts_ctx);
168 }
169 
170 #if defined(CFG_TA_STATS)
171 TEE_Result tee_ta_instance_stats(void *buff, uint32_t *buff_size);
172 #endif
173 
174 #endif
175