xref: /optee_os/core/include/kernel/tee_ta_manager.h (revision 00b3b9a25e768c935ac2ed2fa8f50157390e7204)
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 <kernel/ts_manager.h>
13 #include <mm/tee_mmu_types.h>
14 #include <sys/queue.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 	struct ts_session ts_sess;
98 	uint32_t id;		/* Session handle (0 is invalid) */
99 	TEE_Identity clnt_id;	/* Identify of client */
100 	bool cancel;		/* True if TA invocation is cancelled */
101 	bool cancel_mask;	/* True if cancel is masked */
102 	TEE_Time cancel_time;	/* Time when to cancel the TA invocation */
103 	void *user_ctx;		/* Opaque session handle assigned by PTA */
104 	uint32_t ref_count;	/* reference counter */
105 	struct condvar refc_cv;	/* CV used to wait for ref_count to be 0 */
106 	struct condvar lock_cv;	/* CV used to wait for lock */
107 	short int lock_thread;	/* Id of thread holding the lock */
108 	bool unlink;		/* True if session is to be unlinked */
109 };
110 
111 /* Registered contexts */
112 extern struct tee_ta_ctx_head tee_ctxes;
113 
114 extern struct mutex tee_ta_mutex;
115 extern struct condvar tee_ta_init_cv;
116 
117 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
118 			       struct tee_ta_session **sess,
119 			       struct tee_ta_session_head *open_sessions,
120 			       const TEE_UUID *uuid,
121 			       const TEE_Identity *clnt_id,
122 			       uint32_t cancel_req_to,
123 			       struct tee_ta_param *param);
124 
125 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err,
126 				 struct tee_ta_session *sess,
127 				 const TEE_Identity *clnt_id,
128 				 uint32_t cancel_req_to, uint32_t cmd,
129 				 struct tee_ta_param *param);
130 
131 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err,
132 				 struct tee_ta_session *sess,
133 				 const TEE_Identity *clnt_id);
134 
135 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time);
136 
137 /*-----------------------------------------------------------------------------
138  * Function called to close a TA.
139  * Parameters:
140  * id   - The session id (in)
141  * Returns:
142  *        TEE_Result
143  *---------------------------------------------------------------------------*/
144 TEE_Result tee_ta_close_session(struct tee_ta_session *sess,
145 				struct tee_ta_session_head *open_sessions,
146 				const TEE_Identity *clnt_id);
147 
148 
149 
150 struct tee_ta_session *tee_ta_find_session(uint32_t id,
151 			struct tee_ta_session_head *open_sessions);
152 
153 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
154 			struct tee_ta_session_head *open_sessions);
155 
156 void tee_ta_put_session(struct tee_ta_session *sess);
157 
158 #if defined(CFG_TA_GPROF_SUPPORT)
159 void tee_ta_update_session_utime_suspend(void);
160 void tee_ta_update_session_utime_resume(void);
161 void tee_ta_gprof_sample_pc(vaddr_t pc);
162 #else
163 static inline void tee_ta_update_session_utime_suspend(void) {}
164 static inline void tee_ta_update_session_utime_resume(void) {}
165 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {}
166 #endif
167 #if defined(CFG_FTRACE_SUPPORT)
168 void tee_ta_ftrace_update_times_suspend(void);
169 void tee_ta_ftrace_update_times_resume(void);
170 #else
171 static inline void tee_ta_ftrace_update_times_suspend(void) {}
172 static inline void tee_ta_ftrace_update_times_resume(void) {}
173 #endif
174 
175 static inline struct tee_ta_session * __noprof
176 to_ta_session(struct ts_session *sess)
177 {
178 	return container_of(sess, struct tee_ta_session, ts_sess);
179 }
180 
181 #endif
182