xref: /optee_os/core/include/kernel/tee_ta_manager.h (revision 4576dbb38a6f39534ba4a385eb647b61209f440c)
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 <kernel/mutex.h>
11 #include <kernel/tee_common.h>
12 #include <mm/tee_mmu_types.h>
13 #include <sys/queue.h>
14 #include <tee_api_types.h>
15 #include <tee_api_types.h>
16 #include <types_ext.h>
17 #include <user_ta_header.h>
18 #include <utee_types.h>
19 
20 /* Magic TEE identity pointer: set when teecore requests a TA close */
21 #define KERN_IDENTITY	((TEE_Identity *)-1)
22 /* Operation is initiated by a client (non-secure) app */
23 #define NSAPP_IDENTITY	(NULL)
24 
25 TAILQ_HEAD(tee_ta_session_head, tee_ta_session);
26 TAILQ_HEAD(tee_ta_ctx_head, tee_ta_ctx);
27 
28 struct mobj;
29 
30 struct param_val {
31 	uint32_t a;
32 	uint32_t b;
33 };
34 
35 struct param_mem {
36 	struct mobj *mobj;
37 	size_t size;
38 	size_t offs;
39 };
40 
41 struct tee_ta_param {
42 	uint32_t types;
43 	union {
44 		struct param_val val;
45 		struct param_mem mem;
46 	} u[TEE_NUM_PARAMS];
47 };
48 
49 struct tee_ta_ctx;
50 struct user_ta_ctx;
51 struct pseudo_ta_ctx;
52 struct thread_svc_regs;
53 
54 struct tee_ta_ops {
55 	TEE_Result (*enter_open_session)(struct tee_ta_session *s,
56 			struct tee_ta_param *param, TEE_ErrorOrigin *eo);
57 	TEE_Result (*enter_invoke_cmd)(struct tee_ta_session *s, uint32_t cmd,
58 			struct tee_ta_param *param, TEE_ErrorOrigin *eo);
59 	void (*enter_close_session)(struct tee_ta_session *s);
60 	void (*dump_state)(struct tee_ta_ctx *ctx);
61 	void (*dump_ftrace)(struct tee_ta_ctx *ctx);
62 	void (*destroy)(struct tee_ta_ctx *ctx);
63 	uint32_t (*get_instance_id)(struct tee_ta_ctx *ctx);
64 	bool (*handle_svc)(struct thread_svc_regs *regs);
65 };
66 
67 #if defined(CFG_TA_GPROF_SUPPORT)
68 struct sample_buf {
69 	uint32_t nsamples;	/* Size of @samples array in uint16_t */
70 	uint32_t offset;	/* Passed from user mode */
71 	uint32_t scale;		/* Passed from user mode */
72 	uint32_t count;		/* Number of samples taken */
73 	bool enabled;		/* Sampling enabled? */
74 	uint16_t *samples;
75 	uint64_t usr;		/* Total user CPU time for this session */
76 	uint64_t usr_entered;	/* When this session last entered user mode */
77 	uint32_t freq;		/* @usr divided by @freq is in seconds */
78 };
79 #endif
80 
81 /* Context of a loaded TA */
82 struct tee_ta_ctx {
83 	TEE_UUID uuid;
84 	const struct tee_ta_ops *ops;
85 	uint32_t flags;		/* TA_FLAGS from TA header */
86 	TAILQ_ENTRY(tee_ta_ctx) link;
87 	uint32_t panicked;	/* True if TA has panicked, written from asm */
88 	uint32_t panic_code;	/* Code supplied for panic */
89 	uint32_t ref_count;	/* Reference counter for multi session TA */
90 	bool busy;		/* Context is busy and cannot be entered */
91 	bool initializing;	/* Context is initializing */
92 	struct condvar busy_cv;	/* CV used when context is busy */
93 };
94 
95 struct tee_ta_session {
96 	TAILQ_ENTRY(tee_ta_session) link;
97 	TAILQ_ENTRY(tee_ta_session) link_tsd;
98 	uint32_t id;		/* Session handle (0 is invalid) */
99 	struct tee_ta_ctx *ctx;	/* TA context */
100 	TEE_Identity clnt_id;	/* Identify of client */
101 	bool cancel;		/* True if TA invocation is cancelled */
102 	bool cancel_mask;	/* True if cancel is masked */
103 	TEE_Time cancel_time;	/* Time when to cancel the TA invocation */
104 	void *user_ctx;		/* Opaque session handle assigned by PTA */
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 #if defined(CFG_TA_GPROF_SUPPORT)
111 	struct sample_buf *sbuf; /* Profiling data (PC sampling) */
112 #endif
113 #if defined(CFG_FTRACE_SUPPORT)
114 	struct ftrace_buf *fbuf; /* ftrace buffer */
115 #endif
116 };
117 
118 /* Registered contexts */
119 extern struct tee_ta_ctx_head tee_ctxes;
120 
121 extern struct mutex tee_ta_mutex;
122 
123 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
124 			       struct tee_ta_session **sess,
125 			       struct tee_ta_session_head *open_sessions,
126 			       const TEE_UUID *uuid,
127 			       const TEE_Identity *clnt_id,
128 			       uint32_t cancel_req_to,
129 			       struct tee_ta_param *param);
130 
131 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err,
132 				 struct tee_ta_session *sess,
133 				 const TEE_Identity *clnt_id,
134 				 uint32_t cancel_req_to, uint32_t cmd,
135 				 struct tee_ta_param *param);
136 
137 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err,
138 				 struct tee_ta_session *sess,
139 				 const TEE_Identity *clnt_id);
140 
141 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time);
142 
143 /*-----------------------------------------------------------------------------
144  * Function called to close a TA.
145  * Parameters:
146  * id   - The session id (in)
147  * Returns:
148  *        TEE_Result
149  *---------------------------------------------------------------------------*/
150 TEE_Result tee_ta_close_session(struct tee_ta_session *sess,
151 				struct tee_ta_session_head *open_sessions,
152 				const TEE_Identity *clnt_id);
153 
154 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess);
155 
156 void tee_ta_push_current_session(struct tee_ta_session *sess);
157 struct tee_ta_session *tee_ta_pop_current_session(void);
158 
159 struct tee_ta_session *tee_ta_get_calling_session(void);
160 
161 struct tee_ta_session *tee_ta_find_session(uint32_t id,
162 			struct tee_ta_session_head *open_sessions);
163 
164 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
165 			struct tee_ta_session_head *open_sessions);
166 
167 void tee_ta_put_session(struct tee_ta_session *sess);
168 
169 #if defined(CFG_TA_GPROF_SUPPORT)
170 void tee_ta_update_session_utime_suspend(void);
171 void tee_ta_update_session_utime_resume(void);
172 void tee_ta_gprof_sample_pc(vaddr_t pc);
173 #else
174 static inline void tee_ta_update_session_utime_suspend(void) {}
175 static inline void tee_ta_update_session_utime_resume(void) {}
176 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {}
177 #endif
178 #if defined(CFG_FTRACE_SUPPORT)
179 void tee_ta_ftrace_update_times_suspend(void);
180 void tee_ta_ftrace_update_times_resume(void);
181 #else
182 static inline void tee_ta_ftrace_update_times_suspend(void) {}
183 static inline void tee_ta_ftrace_update_times_resume(void) {}
184 #endif
185 
186 #endif
187