xref: /optee_os/core/include/kernel/tee_ta_manager.h (revision c2f5808039471d8cb9ac43385b63fb8dc6aa8ac4)
1 /*
2  * Copyright (c) 2014, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef TEE_TA_MANAGER_H
29 #define TEE_TA_MANAGER_H
30 
31 #include <types_ext.h>
32 #include <sys/queue.h>
33 #include <tee_api_types.h>
34 #include <utee_types.h>
35 #include <kernel/tee_common.h>
36 #include <kernel/tee_common_unpg.h>
37 #include <kernel/mutex.h>
38 #include <tee_api_types.h>
39 #include <user_ta_header.h>
40 
41 /* Magic TEE identity pointer: set when teecore requests a TA close */
42 #define KERN_IDENTITY	((TEE_Identity *)-1)
43 /* Operation is initiated by a client (non-secure) app */
44 #define NSAPP_IDENTITY	(NULL)
45 
46 TAILQ_HEAD(tee_ta_session_head, tee_ta_session);
47 TAILQ_HEAD(tee_ta_ctx_head, tee_ta_ctx);
48 
49 struct tee_ta_param {
50 	uint32_t types;
51 	TEE_Param params[4];
52 	uint32_t param_attr[4];
53 };
54 
55 struct tee_ta_ctx;
56 struct user_ta_ctx;
57 struct static_ta_ctx;
58 
59 struct tee_ta_ops {
60 	TEE_Result (*enter_open_session)(struct tee_ta_session *s,
61 			struct tee_ta_param *param, TEE_ErrorOrigin *eo);
62 	TEE_Result (*enter_invoke_cmd)(struct tee_ta_session *s, uint32_t cmd,
63 			struct tee_ta_param *param, TEE_ErrorOrigin *eo);
64 	void (*enter_close_session)(struct tee_ta_session *s);
65 	void (*dump_state)(struct tee_ta_ctx *ctx);
66 	void (*destroy)(struct tee_ta_ctx *ctx);
67 };
68 
69 /* Context of a loaded TA */
70 struct tee_ta_ctx {
71 	TEE_UUID uuid;
72 	const struct tee_ta_ops *ops;
73 	uint32_t flags;		/* TA_FLAGS from TA header */
74 	TAILQ_ENTRY(tee_ta_ctx) link;
75 	uint32_t panicked;	/* True if TA has panicked, written from asm */
76 	uint32_t panic_code;	/* Code supplied for panic */
77 	uint32_t ref_count;	/* Reference counter for multi session TA */
78 	bool busy;		/* context is busy and cannot be entered */
79 	struct condvar busy_cv;	/* CV used when context is busy */
80 };
81 
82 struct tee_ta_session {
83 	TAILQ_ENTRY(tee_ta_session) link;
84 	TAILQ_ENTRY(tee_ta_session) link_tsd;
85 	struct tee_ta_ctx *ctx;	/* TA context */
86 	TEE_Identity clnt_id;	/* Identify of client */
87 	bool cancel;		/* True if TAF is cancelled */
88 	bool cancel_mask;	/* True if cancel is masked */
89 	TEE_Time cancel_time;	/* Time when to cancel the TAF */
90 	void *user_ctx;		/* ??? */
91 	uint32_t ref_count;	/* reference counter */
92 	struct condvar refc_cv;	/* CV used to wait for ref_count to be 0 */
93 	struct condvar lock_cv;	/* CV used to wait for lock */
94 	int lock_thread;	/* Id of thread holding the lock */
95 	bool unlink;		/* True if session is to be unlinked */
96 };
97 
98 /* Registered contexts */
99 extern struct tee_ta_ctx_head tee_ctxes;
100 
101 extern struct mutex tee_ta_mutex;
102 
103 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
104 			       struct tee_ta_session **sess,
105 			       struct tee_ta_session_head *open_sessions,
106 			       const TEE_UUID *uuid,
107 			       const TEE_Identity *clnt_id,
108 			       uint32_t cancel_req_to,
109 			       struct tee_ta_param *param);
110 
111 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err,
112 				 struct tee_ta_session *sess,
113 				 const TEE_Identity *clnt_id,
114 				 uint32_t cancel_req_to, uint32_t cmd,
115 				 struct tee_ta_param *param);
116 
117 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err,
118 				 struct tee_ta_session *sess,
119 				 const TEE_Identity *clnt_id);
120 
121 bool tee_ta_session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time);
122 
123 /*-----------------------------------------------------------------------------
124  * Function called to close a TA.
125  * Parameters:
126  * id   - The session id (in)
127  * Returns:
128  *        TEE_Result
129  *---------------------------------------------------------------------------*/
130 TEE_Result tee_ta_close_session(struct tee_ta_session *sess,
131 				struct tee_ta_session_head *open_sessions,
132 				const TEE_Identity *clnt_id);
133 
134 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess);
135 
136 void tee_ta_push_current_session(struct tee_ta_session *sess);
137 struct tee_ta_session *tee_ta_pop_current_session(void);
138 
139 struct tee_ta_session *tee_ta_get_calling_session(void);
140 
141 TEE_Result tee_ta_get_client_id(TEE_Identity *id);
142 
143 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
144 			struct tee_ta_session_head *open_sessions);
145 
146 void tee_ta_put_session(struct tee_ta_session *sess);
147 
148 void tee_ta_dump_current(void);
149 
150 /*
151  * Implemented under core/arch for architecure specific checks
152  */
153 TEE_Result tee_ta_verify_param(struct tee_ta_session *sess,
154 			       struct tee_ta_param *param);
155 
156 #endif
157