xref: /optee_os/core/include/kernel/tee_ta_manager.h (revision 7892cb1bcf8618990ed87458b898b37d6351428f)
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 	struct tee_ta_ctx *ctx;	/* TA context */
85 	/* session of calling TA if != NULL */
86 	struct tee_ta_session *calling_sess;
87 	TEE_Identity clnt_id;	/* Identify of client */
88 	bool cancel;		/* True if TAF is cancelled */
89 	bool cancel_mask;	/* True if cancel is masked */
90 	TEE_Time cancel_time;	/* Time when to cancel the TAF */
91 	void *user_ctx;		/* ??? */
92 	uint32_t ref_count;	/* reference counter */
93 	struct condvar refc_cv;	/* CV used to wait for ref_count to be 0 */
94 	struct condvar lock_cv;	/* CV used to wait for lock */
95 	int lock_thread;	/* Id of thread holding the lock */
96 	bool unlink;		/* True if session is to be unlinked */
97 };
98 
99 /* Registered contexts */
100 extern struct tee_ta_ctx_head tee_ctxes;
101 
102 TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err,
103 			       struct tee_ta_session **sess,
104 			       struct tee_ta_session_head *open_sessions,
105 			       const TEE_UUID *uuid,
106 			       const TEE_Identity *clnt_id,
107 			       uint32_t cancel_req_to,
108 			       struct tee_ta_param *param);
109 
110 TEE_Result tee_ta_invoke_command(TEE_ErrorOrigin *err,
111 				 struct tee_ta_session *sess,
112 				 const TEE_Identity *clnt_id,
113 				 uint32_t cancel_req_to, uint32_t cmd,
114 				 struct tee_ta_param *param);
115 
116 TEE_Result tee_ta_cancel_command(TEE_ErrorOrigin *err,
117 				 struct tee_ta_session *sess,
118 				 const TEE_Identity *clnt_id);
119 
120 /*-----------------------------------------------------------------------------
121  * Function called to close a TA.
122  * Parameters:
123  * id   - The session id (in)
124  * Returns:
125  *        TEE_Result
126  *---------------------------------------------------------------------------*/
127 TEE_Result tee_ta_close_session(struct tee_ta_session *sess,
128 				struct tee_ta_session_head *open_sessions,
129 				const TEE_Identity *clnt_id);
130 
131 TEE_Result tee_ta_get_current_session(struct tee_ta_session **sess);
132 
133 void tee_ta_set_current_session(struct tee_ta_session *sess);
134 
135 TEE_Result tee_ta_get_client_id(TEE_Identity *id);
136 
137 struct tee_ta_session *tee_ta_get_session(uint32_t id, bool exclusive,
138 			struct tee_ta_session_head *open_sessions);
139 
140 void tee_ta_put_session(struct tee_ta_session *sess);
141 
142 void tee_ta_dump_current(void);
143 
144 #endif
145