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