xref: /optee_os/core/include/kernel/tee_ta_manager.h (revision b8bb0afa738e6038bbd92b57742aa2526df9f20a)
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 	struct tee_ta_ctx *ctx;	/* TA context */
94 	TEE_Identity clnt_id;	/* Identify of client */
95 	bool cancel;		/* True if TAF is cancelled */
96 	bool cancel_mask;	/* True if cancel is masked */
97 	TEE_Time cancel_time;	/* Time when to cancel the TAF */
98 	void *user_ctx;		/* ??? */
99 	uint32_t ref_count;	/* reference counter */
100 	struct condvar refc_cv;	/* CV used to wait for ref_count to be 0 */
101 	struct condvar lock_cv;	/* CV used to wait for lock */
102 	int lock_thread;	/* Id of thread holding the lock */
103 	bool unlink;		/* True if session is to be unlinked */
104 #if defined(CFG_TA_GPROF_SUPPORT)
105 	struct sample_buf *sbuf; /* Profiling data (PC sampling) */
106 #endif
107 };
108 
109 /* Registered contexts */
110 extern struct tee_ta_ctx_head tee_ctxes;
111 
112 extern struct mutex tee_ta_mutex;
113 
114 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
115 			       struct tee_ta_session **sess,
116 			       struct tee_ta_session_head *open_sessions,
117 			       const TEE_UUID *uuid,
118 			       const TEE_Identity *clnt_id,
119 			       uint32_t cancel_req_to,
120 			       struct tee_ta_param *param);
121 
122 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err,
123 				 struct tee_ta_session *sess,
124 				 const TEE_Identity *clnt_id,
125 				 uint32_t cancel_req_to, uint32_t cmd,
126 				 struct tee_ta_param *param);
127 
128 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err,
129 				 struct tee_ta_session *sess,
130 				 const TEE_Identity *clnt_id);
131 
132 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time);
133 
134 /*-----------------------------------------------------------------------------
135  * Function called to close a TA.
136  * Parameters:
137  * id   - The session id (in)
138  * Returns:
139  *        TEE_Result
140  *---------------------------------------------------------------------------*/
141 TEE_Result tee_ta_close_session(struct tee_ta_session *sess,
142 				struct tee_ta_session_head *open_sessions,
143 				const TEE_Identity *clnt_id);
144 
145 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess);
146 
147 void tee_ta_push_current_session(struct tee_ta_session *sess);
148 struct tee_ta_session *tee_ta_pop_current_session(void);
149 
150 struct tee_ta_session *tee_ta_get_calling_session(void);
151 
152 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
153 			struct tee_ta_session_head *open_sessions);
154 
155 void tee_ta_put_session(struct tee_ta_session *sess);
156 
157 void tee_ta_dump_current(void);
158 
159 #if defined(CFG_TA_GPROF_SUPPORT)
160 void tee_ta_gprof_sample_pc(vaddr_t pc);
161 void tee_ta_update_session_utime_suspend(void);
162 void tee_ta_update_session_utime_resume(void);
163 #else
164 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {}
165 static inline void tee_ta_update_session_utime_suspend(void) {}
166 static inline void tee_ta_update_session_utime_resume(void) {}
167 #endif
168 
169 #endif
170