xref: /optee_os/core/include/kernel/tee_ta_manager.h (revision d1911a85142da16fef5ebdcdac0348d29ce37cd8)
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 (*dump_state_buffer)(struct tee_ta_ctx *ctx, char *buf,
60 				  size_t *sz);
61 	void (*destroy)(struct tee_ta_ctx *ctx);
62 	uint32_t (*get_instance_id)(struct tee_ta_ctx *ctx);
63 };
64 
65 #if defined(CFG_TA_GPROF_SUPPORT)
66 struct sample_buf {
67 	uint32_t nsamples;	/* Size of @samples array in uint16_t */
68 	uint32_t offset;	/* Passed from user mode */
69 	uint32_t scale;		/* Passed from user mode */
70 	uint32_t count;		/* Number of samples taken */
71 	bool enabled;		/* Sampling enabled? */
72 	uint16_t *samples;
73 	uint64_t usr;		/* Total user CPU time for this session */
74 	uint64_t usr_entered;	/* When this session last entered user mode */
75 	uint32_t freq;		/* @usr divided by @freq is in seconds */
76 };
77 #endif
78 
79 /* Context of a loaded TA */
80 struct tee_ta_ctx {
81 	TEE_UUID uuid;
82 	const struct tee_ta_ops *ops;
83 	uint32_t flags;		/* TA_FLAGS from TA header */
84 	TAILQ_ENTRY(tee_ta_ctx) link;
85 	uint32_t panicked;	/* True if TA has panicked, written from asm */
86 	uint32_t panic_code;	/* Code supplied for panic */
87 	uint32_t ref_count;	/* Reference counter for multi session TA */
88 	bool busy;		/* Context is busy and cannot be entered */
89 	bool initializing;	/* Context is initializing */
90 	struct condvar busy_cv;	/* CV used when context is busy */
91 };
92 
93 struct tee_ta_session {
94 	TAILQ_ENTRY(tee_ta_session) link;
95 	TAILQ_ENTRY(tee_ta_session) link_tsd;
96 	uint32_t id;		/* Session handle (0 is invalid) */
97 	struct tee_ta_ctx *ctx;	/* TA context */
98 	TEE_Identity clnt_id;	/* Identify of client */
99 	bool cancel;		/* True if TA invocation is cancelled */
100 	bool cancel_mask;	/* True if cancel is masked */
101 	TEE_Time cancel_time;	/* Time when to cancel the TA invocation */
102 	void *user_ctx;		/* Opaque session handle assigned by PTA */
103 	uint32_t ref_count;	/* reference counter */
104 	struct condvar refc_cv;	/* CV used to wait for ref_count to be 0 */
105 	struct condvar lock_cv;	/* CV used to wait for lock */
106 	int lock_thread;	/* Id of thread holding the lock */
107 	bool unlink;		/* True if session is to be unlinked */
108 #if defined(CFG_TA_GPROF_SUPPORT)
109 	struct sample_buf *sbuf; /* Profiling data (PC sampling) */
110 #endif
111 #if defined(CFG_TA_FTRACE_SUPPORT)
112 	struct ftrace_buf *fbuf; /* ftrace buffer */
113 #endif
114 };
115 
116 /* Registered contexts */
117 extern struct tee_ta_ctx_head tee_ctxes;
118 
119 extern struct mutex tee_ta_mutex;
120 
121 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
122 			       struct tee_ta_session **sess,
123 			       struct tee_ta_session_head *open_sessions,
124 			       const TEE_UUID *uuid,
125 			       const TEE_Identity *clnt_id,
126 			       uint32_t cancel_req_to,
127 			       struct tee_ta_param *param);
128 
129 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err,
130 				 struct tee_ta_session *sess,
131 				 const TEE_Identity *clnt_id,
132 				 uint32_t cancel_req_to, uint32_t cmd,
133 				 struct tee_ta_param *param);
134 
135 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err,
136 				 struct tee_ta_session *sess,
137 				 const TEE_Identity *clnt_id);
138 
139 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time);
140 
141 /*-----------------------------------------------------------------------------
142  * Function called to close a TA.
143  * Parameters:
144  * id   - The session id (in)
145  * Returns:
146  *        TEE_Result
147  *---------------------------------------------------------------------------*/
148 TEE_Result tee_ta_close_session(struct tee_ta_session *sess,
149 				struct tee_ta_session_head *open_sessions,
150 				const TEE_Identity *clnt_id);
151 
152 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess);
153 
154 void tee_ta_push_current_session(struct tee_ta_session *sess);
155 struct tee_ta_session *tee_ta_pop_current_session(void);
156 
157 struct tee_ta_session *tee_ta_get_calling_session(void);
158 
159 struct tee_ta_session *tee_ta_find_session(uint32_t id,
160 			struct tee_ta_session_head *open_sessions);
161 
162 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
163 			struct tee_ta_session_head *open_sessions);
164 
165 void tee_ta_put_session(struct tee_ta_session *sess);
166 
167 #if defined(CFG_TA_GPROF_SUPPORT)
168 void tee_ta_gprof_sample_pc(vaddr_t pc);
169 void tee_ta_update_session_utime_suspend(void);
170 void tee_ta_update_session_utime_resume(void);
171 #else
172 static inline void tee_ta_gprof_sample_pc(vaddr_t pc __unused) {}
173 static inline void tee_ta_update_session_utime_suspend(void) {}
174 static inline void tee_ta_update_session_utime_resume(void) {}
175 #endif
176 
177 #endif
178